/** * Initializer. * * @param Application $app */ public function __construct(Application $app) { $this->app = $app; $this->site = new Site($app); // Set asset manifest $this->manifest = new AssetManifest($this->app->getEnvironment()); // Set system paths $this->target = $this->app->getTarget(); $this->source = $this->app->getSource(); // Check paths $fs = new Filesystem(); if (!$fs->exists($this->source)) { throw new \Exception("Source folder not found at \"{$this->source}\"."); } // Ensure we have a target if (!$fs->exists($this->target)) { $fs->mkdir($this->target); } // Setup a twig loader $loader = new Twig_Loader_Chain(); $loader->addLoader(new Twig\Loader($this->site)); // If a template directory exists, add a filesystem loader to resolve // templates residing within it $templateDir = $this->source . DIRECTORY_SEPARATOR . "_templates"; if (is_dir($templateDir)) { $loader->addLoader(new Twig_Loader_Filesystem($templateDir)); } $includesDir = $this->source . DIRECTORY_SEPARATOR . "_includes"; if (is_dir($includesDir)) { $loader->addLoader(new Twig_Loader_Filesystem($includesDir)); } // Full template rendering $this->twig = new Twig_Environment($loader, ['debug' => true]); // Make the site variable global $this->twig->addGlobal('site', $this->site); // Add an escaper for XML $this->twig->getExtension('core')->setEscaper('xml', function ($env, $content) { return htmlentities($content, ENT_COMPAT | ENT_XML1); }); // Add build in extensions $this->twig->addExtension(new Twig\Extension($this)); $this->registerTwigExtensions(); // Fire booted event Event::fire('builder.booted', [$this]); }