Engine.php
244 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\League\Plates; |
| 4 | |
| 5 | use IAWP\League\Plates\Extension\ExtensionInterface; |
| 6 | use IAWP\League\Plates\Template\Data; |
| 7 | use IAWP\League\Plates\Template\Directory; |
| 8 | use IAWP\League\Plates\Template\FileExtension; |
| 9 | use IAWP\League\Plates\Template\Folders; |
| 10 | use IAWP\League\Plates\Template\Func; |
| 11 | use IAWP\League\Plates\Template\Functions; |
| 12 | use IAWP\League\Plates\Template\Name; |
| 13 | use IAWP\League\Plates\Template\Template; |
| 14 | /** |
| 15 | * Template API and environment settings storage. |
| 16 | */ |
| 17 | class Engine |
| 18 | { |
| 19 | /** |
| 20 | * Default template directory. |
| 21 | * @var Directory |
| 22 | */ |
| 23 | protected $directory; |
| 24 | /** |
| 25 | * Template file extension. |
| 26 | * @var FileExtension |
| 27 | */ |
| 28 | protected $fileExtension; |
| 29 | /** |
| 30 | * Collection of template folders. |
| 31 | * @var Folders |
| 32 | */ |
| 33 | protected $folders; |
| 34 | /** |
| 35 | * Collection of template functions. |
| 36 | * @var Functions |
| 37 | */ |
| 38 | protected $functions; |
| 39 | /** |
| 40 | * Collection of preassigned template data. |
| 41 | * @var Data |
| 42 | */ |
| 43 | protected $data; |
| 44 | /** |
| 45 | * Create new Engine instance. |
| 46 | * @param string $directory |
| 47 | * @param string $fileExtension |
| 48 | */ |
| 49 | public function __construct($directory = null, $fileExtension = 'php') |
| 50 | { |
| 51 | $this->directory = new Directory($directory); |
| 52 | $this->fileExtension = new FileExtension($fileExtension); |
| 53 | $this->folders = new Folders(); |
| 54 | $this->functions = new Functions(); |
| 55 | $this->data = new Data(); |
| 56 | } |
| 57 | /** |
| 58 | * Set path to templates directory. |
| 59 | * @param string|null $directory Pass null to disable the default directory. |
| 60 | * @return Engine |
| 61 | */ |
| 62 | public function setDirectory($directory) |
| 63 | { |
| 64 | $this->directory->set($directory); |
| 65 | return $this; |
| 66 | } |
| 67 | /** |
| 68 | * Get path to templates directory. |
| 69 | * @return string |
| 70 | */ |
| 71 | public function getDirectory() |
| 72 | { |
| 73 | return $this->directory->get(); |
| 74 | } |
| 75 | /** |
| 76 | * Set the template file extension. |
| 77 | * @param string|null $fileExtension Pass null to manually set it. |
| 78 | * @return Engine |
| 79 | */ |
| 80 | public function setFileExtension($fileExtension) |
| 81 | { |
| 82 | $this->fileExtension->set($fileExtension); |
| 83 | return $this; |
| 84 | } |
| 85 | /** |
| 86 | * Get the template file extension. |
| 87 | * @return string |
| 88 | */ |
| 89 | public function getFileExtension() |
| 90 | { |
| 91 | return $this->fileExtension->get(); |
| 92 | } |
| 93 | /** |
| 94 | * Add a new template folder for grouping templates under different namespaces. |
| 95 | * @param string $name |
| 96 | * @param string $directory |
| 97 | * @param boolean $fallback |
| 98 | * @return Engine |
| 99 | */ |
| 100 | public function addFolder($name, $directory, $fallback = \false) |
| 101 | { |
| 102 | $this->folders->add($name, $directory, $fallback); |
| 103 | return $this; |
| 104 | } |
| 105 | /** |
| 106 | * Remove a template folder. |
| 107 | * @param string $name |
| 108 | * @return Engine |
| 109 | */ |
| 110 | public function removeFolder($name) |
| 111 | { |
| 112 | $this->folders->remove($name); |
| 113 | return $this; |
| 114 | } |
| 115 | /** |
| 116 | * Get collection of all template folders. |
| 117 | * @return Folders |
| 118 | */ |
| 119 | public function getFolders() |
| 120 | { |
| 121 | return $this->folders; |
| 122 | } |
| 123 | /** |
| 124 | * Add preassigned template data. |
| 125 | * @param array $data; |
| 126 | * @param null|string|array $templates; |
| 127 | * @return Engine |
| 128 | */ |
| 129 | public function addData(array $data, $templates = null) |
| 130 | { |
| 131 | $this->data->add($data, $templates); |
| 132 | return $this; |
| 133 | } |
| 134 | /** |
| 135 | * Get all preassigned template data. |
| 136 | * @param null|string $template; |
| 137 | * @return array |
| 138 | */ |
| 139 | public function getData($template = null) |
| 140 | { |
| 141 | return $this->data->get($template); |
| 142 | } |
| 143 | /** |
| 144 | * Register a new template function. |
| 145 | * @param string $name; |
| 146 | * @param callback $callback; |
| 147 | * @return Engine |
| 148 | */ |
| 149 | public function registerFunction($name, $callback) |
| 150 | { |
| 151 | $this->functions->add($name, $callback); |
| 152 | return $this; |
| 153 | } |
| 154 | /** |
| 155 | * Remove a template function. |
| 156 | * @param string $name; |
| 157 | * @return Engine |
| 158 | */ |
| 159 | public function dropFunction($name) |
| 160 | { |
| 161 | $this->functions->remove($name); |
| 162 | return $this; |
| 163 | } |
| 164 | /** |
| 165 | * Get a template function. |
| 166 | * @param string $name |
| 167 | * @return Func |
| 168 | */ |
| 169 | public function getFunction($name) |
| 170 | { |
| 171 | return $this->functions->get($name); |
| 172 | } |
| 173 | /** |
| 174 | * Check if a template function exists. |
| 175 | * @param string $name |
| 176 | * @return boolean |
| 177 | */ |
| 178 | public function doesFunctionExist($name) |
| 179 | { |
| 180 | return $this->functions->exists($name); |
| 181 | } |
| 182 | /** |
| 183 | * Load an extension. |
| 184 | * @param ExtensionInterface $extension |
| 185 | * @return Engine |
| 186 | */ |
| 187 | public function loadExtension(ExtensionInterface $extension) |
| 188 | { |
| 189 | $extension->register($this); |
| 190 | return $this; |
| 191 | } |
| 192 | /** |
| 193 | * Load multiple extensions. |
| 194 | * @param array $extensions |
| 195 | * @return Engine |
| 196 | */ |
| 197 | public function loadExtensions(array $extensions = array()) |
| 198 | { |
| 199 | foreach ($extensions as $extension) { |
| 200 | $this->loadExtension($extension); |
| 201 | } |
| 202 | return $this; |
| 203 | } |
| 204 | /** |
| 205 | * Get a template path. |
| 206 | * @param string $name |
| 207 | * @return string |
| 208 | */ |
| 209 | public function path($name) |
| 210 | { |
| 211 | $name = new Name($this, $name); |
| 212 | return $name->getPath(); |
| 213 | } |
| 214 | /** |
| 215 | * Check if a template exists. |
| 216 | * @param string $name |
| 217 | * @return boolean |
| 218 | */ |
| 219 | public function exists($name) |
| 220 | { |
| 221 | $name = new Name($this, $name); |
| 222 | return $name->doesPathExist(); |
| 223 | } |
| 224 | /** |
| 225 | * Create a new template. |
| 226 | * @param string $name |
| 227 | * @return Template |
| 228 | */ |
| 229 | public function make($name) |
| 230 | { |
| 231 | return new Template($this, $name); |
| 232 | } |
| 233 | /** |
| 234 | * Create a new template and render it. |
| 235 | * @param string $name |
| 236 | * @param array $data |
| 237 | * @return string |
| 238 | */ |
| 239 | public function render($name, array $data = array()) |
| 240 | { |
| 241 | return $this->make($name)->render($data); |
| 242 | } |
| 243 | } |
| 244 |