/** * {@inheritdoc} * * @param Isolator $isolator Custom PHP isolator instance. */ public function process($source, Isolator $isolator = null) { $isolator = !empty($isolator) ? $isolator : new Isolator(); //Real php source code isolation $source = $isolator->isolatePHP($source); //Restoring only evaluator blocks $phpBlocks = $evaluateBlocks = []; foreach ($isolator->getBlocks() as $id => $phpBlock) { foreach ($this->options['flags'] as $flag) { if (strpos($phpBlock, $flag) !== false) { $evaluateBlocks[$id] = $phpBlock; continue 2; } } $phpBlocks[$id] = $phpBlock; } $source = $isolator->setBlocks($evaluateBlocks)->repairPHP($source); $isolator->setBlocks($phpBlocks); //Required to prevent collisions $filename = $this->views->config()['cache']['directory'] . "/{$this->uniqueID()}.php"; try { $this->files->write($filename, $source, FilesInterface::RUNTIME, true); ob_start(); include_once $filename; $source = ob_get_clean(); $this->files->delete($filename); } catch (\ErrorException $exception) { throw $exception; } return $isolator->repairPHP($source); }
/** * Clone compiler with reconfigured namespace and view. * * @param string $namespace * @param string $view * @return Compiler */ public function reconfigure($namespace, $view) { $compiler = clone $this; $compiler->namespace = $namespace; $compiler->view = $view; //Must be the same engine $compiler->filename = $this->views->getFilename($namespace, $view); $compiler->compiledFilename = ''; //Processors has to be regenerated to flush content $compiler->processors = []; return $compiler; }
/** * @param ViewLocator $locator * @param ViewManager $manager * @param ConsoleDispatcher $dispatcher */ public function perform(ViewLocator $locator, ViewManager $manager, ConsoleDispatcher $dispatcher) { //To clean up cache $dispatcher->command('views:reset', [], $this->output); if ($this->isVerbosity()) { $this->write("\n"); } /** * @var FormatterHelper $formatter */ $formatter = $this->getHelper('formatter'); foreach ($locator->getNamespaces() as $namespace) { $this->isVerbosity() && $this->writeln("Compiling views in namespace '<comment>{$namespace}</comment>'."); foreach ($locator->namespaceViews($namespace) as $view => $engine) { if ($this->isVerbosity()) { $this->write($formatter->formatSection("{$namespace}:{$engine}", $view . ", ", 'fg=cyan')); } $benchmark = $this->benchmark('compile'); try { //Compilation $manager->engine($engine)->compile("{$namespace}:{$view}", true); $this->isVerbosity() && $this->write("<info>ok</info>"); } catch (\Exception $exception) { if ($this->isVerbosity()) { $this->write("<fg=red>error: {$exception->getMessage()}</fg=red>"); } } finally { $elapsed = number_format($this->benchmark($benchmark) * 1000); if ($this->isVerbosity()) { $this->writeln(" <comment>[{$elapsed} ms]</comment> "); } } } } $this->writeln("<info>View cache was successfully generated.</info>"); }
/** * Mount profiler panel to response (if possible). * * @param Request $request Server request instance. * @param Response $response * @param float $started Time when profiler was activated. * @param float $elapsed Elapsed time. * @return Response */ protected function mountPanel(Request $request, Response $response, $started = 0.0, $elapsed = 0.0) { if (!$response->getBody()->isWritable()) { //We can't write to the stream return $response; } if (!empty($response->getHeaderLine('Content-Type'))) { if (strpos($response->getHeaderLine('Content-Type'), 'html') === false) { //We can only write to responses when content type does not specified or responses //with html related content type return $response; } } $response->getBody()->write($this->view->render('profiler:panel', ['profiler' => $this, 'container' => $this->container, 'request' => $request, 'response' => $response, 'started' => $started, 'elapsed' => $elapsed])); return $response; }
/** * Generate view aliases based on provided view namespace and directory under such namespace. * * @param ViewManager $viewManager * @param TemplateProcessor $templater * @param array $token */ protected function buildAliases(ViewManager $viewManager, TemplateProcessor $templater, array $token) { if (isset(self::$cache[$this->importID()])) { //Already generated $this->aliases = self::$cache[$this->importID()]; return; } try { $views = $viewManager->getViews($this->namespace); } catch (ViewException $exception) { //Unable to generate import throw new TemplaterException($exception->getMessage(), $token, $exception->getCode(), $exception); } foreach ($views as $view => $engine) { if (!empty($this->directory) && strpos($view, $this->directory) !== 0) { //Different directory continue; } //Remove directory from view name $alias = ltrim(substr($view, strlen($this->directory)), '/'); //Replace path separator (must be normalized) with tag path separator (usually .) $alias = str_replace('/', $templater->getOptions()['separator'], $alias); //View alias = namespace:view.subView $alias = $this->outerNamespace . $templater->getOptions()['nsSeparator'] . $alias; $this->aliases[$alias] = $view; } if (empty($this->aliases)) { throw new TemplaterException("No views were found under directory '{$this->directory}' in namespace '{$this->namespace}'.", $token); } //Caching self::$cache[$this->importID()] = $this->aliases; }
/** * @param ViewManager $views * @return \Twig_Environment */ public function twig(ViewManager $views) { return $views->engine('twig')->twig(); }