@@ -36,7 +36,8 @@ |
||
| 36 | 36 | $manager->addEngine(new NativeEngine($this->container, 'dark.php')); |
| 37 | 37 | |
| 38 | 38 | $ext = []; |
| 39 | - foreach ($manager->getEngines() as $e) { |
|
| 39 | + foreach ($manager->getEngines() as $e) |
|
| 40 | + { |
|
| 40 | 41 | $ext[] = $e->getLoader()->getExtension(); |
| 41 | 42 | } |
| 42 | 43 | |
@@ -30,7 +30,8 @@ discard block |
||
| 30 | 30 | public function getID(): string |
| 31 | 31 | { |
| 32 | 32 | $calculated = ''; |
| 33 | - foreach ($this->dependencies as $dependency) { |
|
| 33 | + foreach ($this->dependencies as $dependency) |
|
| 34 | + { |
|
| 34 | 35 | $calculated .= "[{$dependency->getName()}={$dependency->getValue()}]"; |
| 35 | 36 | } |
| 36 | 37 | |
@@ -61,7 +62,8 @@ discard block |
||
| 61 | 62 | */ |
| 62 | 63 | public function resolveValue(string $dependency) |
| 63 | 64 | { |
| 64 | - if (!isset($this->dependencies[$dependency])) { |
|
| 65 | + if (!isset($this->dependencies[$dependency])) |
|
| 66 | + { |
|
| 65 | 67 | throw new ContextException("Undefined context dependency '{$dependency}'."); |
| 66 | 68 | } |
| 67 | 69 | |
@@ -44,15 +44,18 @@ discard block |
||
| 44 | 44 | 'namespaces' => $config->getNamespaces() |
| 45 | 45 | ]); |
| 46 | 46 | |
| 47 | - foreach ($this->config->getDependencies() as $dependency) { |
|
| 47 | + foreach ($this->config->getDependencies() as $dependency) |
|
| 48 | + { |
|
| 48 | 49 | $this->addDependency($dependency->resolve($factory)); |
| 49 | 50 | } |
| 50 | 51 | |
| 51 | - foreach ($this->config->getEngines() as $engine) { |
|
| 52 | + foreach ($this->config->getEngines() as $engine) |
|
| 53 | + { |
|
| 52 | 54 | $this->addEngine($engine->resolve($factory)); |
| 53 | 55 | } |
| 54 | 56 | |
| 55 | - if ($this->config->isCacheEnabled()) { |
|
| 57 | + if ($this->config->isCacheEnabled()) |
|
| 58 | + { |
|
| 56 | 59 | $this->cache = new ViewCache(); |
| 57 | 60 | } |
| 58 | 61 | } |
@@ -84,7 +87,8 @@ discard block |
||
| 84 | 87 | { |
| 85 | 88 | $this->engines[] = $engine->withLoader($this->loader); |
| 86 | 89 | |
| 87 | - uasort($this->engines, function (EngineInterface $a, EngineInterface $b) { |
|
| 90 | + uasort($this->engines, function (EngineInterface $a, EngineInterface $b) |
|
| 91 | + { |
|
| 88 | 92 | return strcmp($a->getLoader()->getExtension(), $b->getLoader()->getExtension()); |
| 89 | 93 | }); |
| 90 | 94 | |
@@ -110,7 +114,8 @@ discard block |
||
| 110 | 114 | */ |
| 111 | 115 | public function compile(string $path): void |
| 112 | 116 | { |
| 113 | - if ($this->cache !== null) { |
|
| 117 | + if ($this->cache !== null) |
|
| 118 | + { |
|
| 114 | 119 | $this->cache->resetPath($path); |
| 115 | 120 | } |
| 116 | 121 | |
@@ -118,7 +123,8 @@ discard block |
||
| 118 | 123 | |
| 119 | 124 | // Rotate all possible context variants and warm up cache |
| 120 | 125 | $generator = new ContextGenerator($this->context); |
| 121 | - foreach ($generator->generate() as $context) { |
|
| 126 | + foreach ($generator->generate() as $context) |
|
| 127 | + { |
|
| 122 | 128 | $engine->reset($path, $context); |
| 123 | 129 | $engine->compile($path, $context); |
| 124 | 130 | } |
@@ -131,7 +137,8 @@ discard block |
||
| 131 | 137 | */ |
| 132 | 138 | public function reset(string $path): void |
| 133 | 139 | { |
| 134 | - if ($this->cache !== null) { |
|
| 140 | + if ($this->cache !== null) |
|
| 141 | + { |
|
| 135 | 142 | $this->cache->resetPath($path); |
| 136 | 143 | } |
| 137 | 144 | |
@@ -139,7 +146,8 @@ discard block |
||
| 139 | 146 | |
| 140 | 147 | // Rotate all possible context variants and warm up cache |
| 141 | 148 | $generator = new ContextGenerator($this->context); |
| 142 | - foreach ($generator->generate() as $context) { |
|
| 149 | + foreach ($generator->generate() as $context) |
|
| 150 | + { |
|
| 143 | 151 | $engine->reset($path, $context); |
| 144 | 152 | } |
| 145 | 153 | } |
@@ -154,13 +162,15 @@ discard block |
||
| 154 | 162 | */ |
| 155 | 163 | public function get(string $path): ViewInterface |
| 156 | 164 | { |
| 157 | - if ($this->cache !== null && $this->cache->has($this->context, $path)) { |
|
| 165 | + if ($this->cache !== null && $this->cache->has($this->context, $path)) |
|
| 166 | + { |
|
| 158 | 167 | return $this->cache->get($this->context, $path); |
| 159 | 168 | } |
| 160 | 169 | |
| 161 | 170 | $view = $this->findEngine($path)->get($path, $this->context); |
| 162 | 171 | |
| 163 | - if ($this->cache !== null) { |
|
| 172 | + if ($this->cache !== null) |
|
| 173 | + { |
|
| 164 | 174 | $this->cache->set($this->context, $path, $view); |
| 165 | 175 | } |
| 166 | 176 | |
@@ -187,8 +197,10 @@ discard block |
||
| 187 | 197 | */ |
| 188 | 198 | protected function findEngine(string $path): EngineInterface |
| 189 | 199 | { |
| 190 | - foreach ($this->engines as $engine) { |
|
| 191 | - if ($engine->getLoader()->exists($path)) { |
|
| 200 | + foreach ($this->engines as $engine) |
|
| 201 | + { |
|
| 202 | + if ($engine->getLoader()->exists($path)) |
|
| 203 | + { |
|
| 192 | 204 | return $engine; |
| 193 | 205 | } |
| 194 | 206 | } |
@@ -29,7 +29,8 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | private function process(ViewSource $source, ContextInterface $context): ViewSource |
| 31 | 31 | { |
| 32 | - foreach ($this->processors as $processor) { |
|
| 32 | + foreach ($this->processors as $processor) |
|
| 33 | + { |
|
| 33 | 34 | $source = $processor->process($source, $context); |
| 34 | 35 | } |
| 35 | 36 | |
@@ -69,7 +69,8 @@ discard block |
||
| 69 | 69 | public function getDependencies(): array |
| 70 | 70 | { |
| 71 | 71 | $dependencies = []; |
| 72 | - foreach ($this->config['dependencies'] as $dependency) { |
|
| 72 | + foreach ($this->config['dependencies'] as $dependency) |
|
| 73 | + { |
|
| 73 | 74 | $dependencies[] = $this->wire($dependency); |
| 74 | 75 | } |
| 75 | 76 | |
@@ -86,7 +87,8 @@ discard block |
||
| 86 | 87 | public function getEngines(): array |
| 87 | 88 | { |
| 88 | 89 | $engines = []; |
| 89 | - foreach ($this->config['engines'] as $engine) { |
|
| 90 | + foreach ($this->config['engines'] as $engine) |
|
| 91 | + { |
|
| 90 | 92 | $engines[] = $this->wire($engine); |
| 91 | 93 | } |
| 92 | 94 | |
@@ -101,11 +103,13 @@ discard block |
||
| 101 | 103 | */ |
| 102 | 104 | private function wire($item): Autowire |
| 103 | 105 | { |
| 104 | - if ($item instanceof Autowire) { |
|
| 106 | + if ($item instanceof Autowire) |
|
| 107 | + { |
|
| 105 | 108 | return $item; |
| 106 | 109 | } |
| 107 | 110 | |
| 108 | - if (is_string($item)) { |
|
| 111 | + if (is_string($item)) |
|
| 112 | + { |
|
| 109 | 113 | return new Autowire($item); |
| 110 | 114 | } |
| 111 | 115 | |
@@ -52,22 +52,26 @@ |
||
| 52 | 52 | */ |
| 53 | 53 | private function rotate(ContextInterface $context, array $dependencies): array |
| 54 | 54 | { |
| 55 | - if (empty($dependencies)) { |
|
| 55 | + if (empty($dependencies)) |
|
| 56 | + { |
|
| 56 | 57 | return []; |
| 57 | 58 | } |
| 58 | 59 | |
| 59 | 60 | $top = array_shift($dependencies); |
| 60 | 61 | |
| 61 | 62 | $variants = []; |
| 62 | - foreach ($top->getVariants() as $value) { |
|
| 63 | + foreach ($top->getVariants() as $value) |
|
| 64 | + { |
|
| 63 | 65 | $variant = $context->withDependency(new ValueDependency($top->getName(), $value)); |
| 64 | 66 | |
| 65 | - if (empty($dependencies)) { |
|
| 67 | + if (empty($dependencies)) |
|
| 68 | + { |
|
| 66 | 69 | $variants[] = $variant; |
| 67 | 70 | continue; |
| 68 | 71 | } |
| 69 | 72 | |
| 70 | - foreach ($this->rotate($variant, $dependencies) as $inner) { |
|
| 73 | + foreach ($this->rotate($variant, $dependencies) as $inner) |
|
| 74 | + { |
|
| 71 | 75 | $variants[] = $inner; |
| 72 | 76 | } |
| 73 | 77 | } |
@@ -44,7 +44,8 @@ |
||
| 44 | 44 | */ |
| 45 | 45 | public function getLoader(): LoaderInterface |
| 46 | 46 | { |
| 47 | - if (empty($this->loader)) { |
|
| 47 | + if (empty($this->loader)) |
|
| 48 | + { |
|
| 48 | 49 | throw new EngineException('No associated loader found'); |
| 49 | 50 | } |
| 50 | 51 | |
@@ -49,21 +49,28 @@ |
||
| 49 | 49 | ob_start(); |
| 50 | 50 | $__outputLevel__ = ob_get_level(); |
| 51 | 51 | |
| 52 | - try { |
|
| 52 | + try |
|
| 53 | + { |
|
| 53 | 54 | ContainerScope::runScope($this->container, function () use ($data): void { |
| 54 | 55 | extract($data, EXTR_OVERWRITE); |
| 55 | 56 | // render view in context and output buffer scope, context can be accessed using $this->context |
| 56 | 57 | require $this->view->getFilename(); |
| 57 | 58 | }); |
| 58 | - } catch (\Throwable $e) { |
|
| 59 | - while (ob_get_level() >= $__outputLevel__) { |
|
| 59 | + } |
|
| 60 | + catch (\Throwable $e) |
|
| 61 | + { |
|
| 62 | + while (ob_get_level() >= $__outputLevel__) |
|
| 63 | + { |
|
| 60 | 64 | ob_end_clean(); |
| 61 | 65 | } |
| 62 | 66 | |
| 63 | 67 | throw new RenderException($e); |
| 64 | - } finally { |
|
| 68 | + } |
|
| 69 | + finally |
|
| 70 | + { |
|
| 65 | 71 | //Closing all nested buffers |
| 66 | - while (ob_get_level() > $__outputLevel__) { |
|
| 72 | + while (ob_get_level() > $__outputLevel__) |
|
| 73 | + { |
|
| 67 | 74 | ob_end_clean(); |
| 68 | 75 | } |
| 69 | 76 | } |
@@ -23,7 +23,8 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | public function reset(ContextInterface $context = null): void |
| 25 | 25 | { |
| 26 | - if (empty($context)) { |
|
| 26 | + if (empty($context)) |
|
| 27 | + { |
|
| 27 | 28 | $this->cache = []; |
| 28 | 29 | return; |
| 29 | 30 | } |
@@ -38,7 +39,8 @@ discard block |
||
| 38 | 39 | */ |
| 39 | 40 | public function resetPath(string $path): void |
| 40 | 41 | { |
| 41 | - foreach ($this->cache as &$cache) { |
|
| 42 | + foreach ($this->cache as &$cache) |
|
| 43 | + { |
|
| 42 | 44 | unset($cache[$path], $cache); |
| 43 | 45 | } |
| 44 | 46 | } |
@@ -72,7 +74,8 @@ discard block |
||
| 72 | 74 | */ |
| 73 | 75 | public function get(ContextInterface $context, string $path): ViewInterface |
| 74 | 76 | { |
| 75 | - if (!$this->has($context, $path)) { |
|
| 77 | + if (!$this->has($context, $path)) |
|
| 78 | + { |
|
| 76 | 79 | throw new CacheException("No cache is available for {$path}."); |
| 77 | 80 | } |
| 78 | 81 | |