|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ACF_Gutenberg\Classes; |
| 4 | + |
| 5 | +use Illuminate\Container\Container; |
| 6 | +use Illuminate\Events\Dispatcher; |
| 7 | +use Illuminate\Filesystem\Filesystem; |
| 8 | +use Illuminate\View\Engines\PhpEngine; |
| 9 | +use Illuminate\View\Engines\CompilerEngine; |
| 10 | +use Illuminate\View\Engines\EngineResolver; |
| 11 | +use Illuminate\View\Compilers\BladeCompiler; |
| 12 | +use Illuminate\View\FileViewFinder; |
| 13 | +use Illuminate\View\Factory; |
| 14 | + |
| 15 | +class Blade |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Array containing paths where to look for blade files |
| 19 | + * @var array |
| 20 | + */ |
| 21 | + public $viewPaths; |
| 22 | + |
| 23 | + /** |
| 24 | + * Location where to store cached views |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + public $cachePath; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var Illuminate\Container\Container |
| 31 | + */ |
| 32 | + protected $container; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var Illuminate\View\Factory |
| 36 | + */ |
| 37 | + protected $instance; |
| 38 | + |
| 39 | + /** |
| 40 | + * Initialize class |
| 41 | + * @param array $viewPaths |
| 42 | + * @param string $cachePath |
| 43 | + * @param Illuminate\Events\Dispatcher $events |
| 44 | + */ |
| 45 | + public function __construct($viewPaths = [], $cachePath, Dispatcher $events = null) |
| 46 | + { |
| 47 | + $this->container = new Container; |
| 48 | + |
| 49 | + $this->viewPaths = (array) $viewPaths; |
| 50 | + |
| 51 | + $this->cachePath = $cachePath; |
| 52 | + |
| 53 | + $this->registerFilesystem(); |
| 54 | + |
| 55 | + $this->registerEvents($events ?: new Dispatcher); |
| 56 | + |
| 57 | + $this->registerEngineResolver(); |
| 58 | + |
| 59 | + $this->registerViewFinder(); |
| 60 | + |
| 61 | + $this->instance = $this->registerFactory(); |
| 62 | + } |
| 63 | + |
| 64 | + public function view() |
| 65 | + { |
| 66 | + return $this->instance; |
| 67 | + } |
| 68 | + |
| 69 | + public function registerFilesystem() |
| 70 | + { |
| 71 | + $this->container->singleton('files', function () { |
| 72 | + return new Filesystem; |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + public function registerEvents(Dispatcher $events) |
| 77 | + { |
| 78 | + $this->container->singleton('events', function () use ($events) { |
| 79 | + return $events; |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Register the engine resolver instance. |
| 85 | + * |
| 86 | + * @return void |
| 87 | + */ |
| 88 | + public function registerEngineResolver() |
| 89 | + { |
| 90 | + $me = $this; |
| 91 | + |
| 92 | + $this->container->singleton('view.engine.resolver', function ($app) use ($me) { |
| 93 | + $resolver = new EngineResolver; |
| 94 | + |
| 95 | + // Next we will register the various engines with the resolver so that the |
| 96 | + // environment can resolve the engines it needs for various views based |
| 97 | + // on the extension of view files. We call a method for each engines. |
| 98 | + foreach (['php', 'blade'] as $engine) { |
| 99 | + $me->{'register' . ucfirst($engine) . 'Engine'}($resolver); |
| 100 | + } |
| 101 | + |
| 102 | + return $resolver; |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Register the PHP engine implementation. |
| 108 | + * |
| 109 | + * @param \Illuminate\View\Engines\EngineResolver $resolver |
| 110 | + * @return void |
| 111 | + */ |
| 112 | + public function registerPhpEngine($resolver) |
| 113 | + { |
| 114 | + $resolver->register('php', function () { return new PhpEngine; }); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Register the Blade engine implementation. |
| 119 | + * |
| 120 | + * @param \Illuminate\View\Engines\EngineResolver $resolver |
| 121 | + * @return void |
| 122 | + */ |
| 123 | + public function registerBladeEngine($resolver) |
| 124 | + { |
| 125 | + $me = $this; |
| 126 | + $app = $this->container; |
| 127 | + |
| 128 | + // The Compiler engine requires an instance of the CompilerInterface, which in |
| 129 | + // this case will be the Blade compiler, so we'll first create the compiler |
| 130 | + // instance to pass into the engine so it can compile the views properly. |
| 131 | + $this->container->singleton('blade.compiler', function ($app) use ($me) { |
| 132 | + $cache = $me->cachePath; |
| 133 | + |
| 134 | + return new BladeCompiler($app['files'], $cache); |
| 135 | + }); |
| 136 | + |
| 137 | + $resolver->register('blade', function () use ($app) { |
| 138 | + return new CompilerEngine($app['blade.compiler'], $app['files']); |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Register the view finder implementation. |
| 144 | + * |
| 145 | + * @return void |
| 146 | + */ |
| 147 | + public function registerViewFinder() |
| 148 | + { |
| 149 | + $me = $this; |
| 150 | + $this->container->singleton('view.finder', function ($app) use ($me) { |
| 151 | + $paths = $me->viewPaths; |
| 152 | + |
| 153 | + return new FileViewFinder($app['files'], $paths); |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Register the view environment. |
| 159 | + * |
| 160 | + * @return void |
| 161 | + */ |
| 162 | + public function registerFactory() |
| 163 | + { |
| 164 | + // Next we need to grab the engine resolver instance that will be used by the |
| 165 | + // environment. The resolver will be used by an environment to get each of |
| 166 | + // the various engine implementations such as plain PHP or Blade engine. |
| 167 | + $resolver = $this->container['view.engine.resolver']; |
| 168 | + |
| 169 | + $finder = $this->container['view.finder']; |
| 170 | + |
| 171 | + $env = new Factory($resolver, $finder, $this->container['events']); |
| 172 | + |
| 173 | + // We will also set the container instance on this view environment since the |
| 174 | + // view composers may be classes registered in the container, which allows |
| 175 | + // for great testable, flexible composers for the application developer. |
| 176 | + $env->setContainer($this->container); |
| 177 | + |
| 178 | + return $env; |
| 179 | + } |
| 180 | + |
| 181 | + public function getCompiler() |
| 182 | + { |
| 183 | + return $this->container['blade.compiler']; |
| 184 | + } |
| 185 | +} |
0 commit comments