public function __construct($kernel)
 {
     if (empty($kernel) || !$kernel instanceof Kernel) {
         throw new \InvalidArgumentException("It seems you did not set the new settings in services.yml, please add \"@kernel\" to templating.engine.pug service arguments, see https://github.com/pug-php/pug-symfony#readme", 1);
     }
     $this->kernel = $kernel;
     $cache = $this->getCacheDir();
     if (!file_exists($cache)) {
         mkdir($cache);
     }
     $container = $kernel->getContainer();
     $this->container = $container;
     $environment = $container->getParameter('kernel.environment');
     $appDir = $container->getParameter('kernel.root_dir');
     $rootDir = dirname($appDir);
     $assetsDirectories = array($appDir . '/Resources/assets');
     $srcDir = $rootDir . '/src';
     $webDir = $rootDir . '/web';
     $baseDir = null;
     foreach (scandir($srcDir) as $directory) {
         if ($directory === '.' || $directory === '..' || is_file($srcDir . '/' . $directory)) {
             continue;
         }
         if (is_null($baseDir) && is_dir($srcDir . '/Resources/views')) {
             $baseDir = $appDir . '/Resources/views';
         }
         $assetsDirectories[] = $srcDir . '/' . $directory . '/Resources/assets';
     }
     if (is_null($baseDir)) {
         $baseDir = $appDir . '/Resources/views';
     }
     $this->jade = new Jade(array('assetDirectory' => $assetsDirectories, 'baseDir' => $baseDir, 'cache' => substr($environment, 0, 3) === 'dev' ? false : $cache, 'environment' => $environment, 'extension' => array('.pug', '.jade'), 'outputDirectory' => $webDir, 'preRender' => array($this, 'preRender'), 'prettyprint' => $kernel->isDebug()));
     $this->registerHelpers($container, array_slice(func_get_args(), 1));
     $this->assets = new Assets($this->jade);
     $app = new AppVariable();
     $app->setDebug($kernel->isDebug());
     $app->setEnvironment($environment);
     $app->setRequestStack($container->get('request_stack'));
     $app->setTokenStorage($container->get('security.token_storage'));
     $this->jade->share(array('app' => $app, 'user' => $app->getUser()));
 }
 public function register(Container $app)
 {
     $app['twig.options'] = array();
     $app['twig.form.templates'] = array('form_div_layout.html.twig');
     $app['twig.path'] = array();
     $app['twig.templates'] = array();
     $app['twig.app_variable'] = function ($app) {
         $var = new AppVariable();
         if (isset($app['security.token_storage'])) {
             $var->setTokenStorage($app['security.token_storage']);
         }
         if (isset($app['request_stack'])) {
             $var->setRequestStack($app['request_stack']);
         }
         $var->setDebug($app['debug']);
         return $var;
     };
     $app['twig'] = function ($app) {
         $app['twig.options'] = array_replace(array('charset' => $app['charset'], 'debug' => $app['debug'], 'strict_variables' => $app['debug']), $app['twig.options']);
         $twig = $app['twig.environment_factory']($app);
         $twig->addGlobal('app', $app['twig.app_variable']);
         if ($app['debug']) {
             $twig->addExtension(new \Twig_Extension_Debug());
         }
         if (class_exists('Symfony\\Bridge\\Twig\\Extension\\RoutingExtension')) {
             if (isset($app['request_stack'])) {
                 $twig->addExtension(new HttpFoundationExtension($app['request_stack']));
                 $twig->addExtension(new RoutingExtension($app['url_generator']));
             }
             if (isset($app['translator'])) {
                 $twig->addExtension(new TranslationExtension($app['translator']));
             }
             if (isset($app['security.authorization_checker'])) {
                 $twig->addExtension(new SecurityExtension($app['security.authorization_checker']));
             }
             if (isset($app['fragment.handler'])) {
                 $app['fragment.renderer.hinclude']->setTemplating($twig);
                 $twig->addExtension(new HttpKernelExtension($app['fragment.handler']));
             }
             if (isset($app['assets.packages'])) {
                 $twig->addExtension(new AssetExtension($app['assets.packages']));
             }
             if (isset($app['form.factory'])) {
                 $app['twig.form.engine'] = function ($app) {
                     return new TwigRendererEngine($app['twig.form.templates']);
                 };
                 $app['twig.form.renderer'] = function ($app) {
                     return new TwigRenderer($app['twig.form.engine'], $app['csrf.token_manager']);
                 };
                 $twig->addExtension(new FormExtension($app['twig.form.renderer']));
                 // add loader for Symfony built-in form templates
                 $reflected = new \ReflectionClass('Symfony\\Bridge\\Twig\\Extension\\FormExtension');
                 $path = dirname($reflected->getFileName()) . '/../Resources/views/Form';
                 $app['twig.loader']->addLoader(new \Twig_Loader_Filesystem($path));
             }
             if (isset($app['var_dumper.cloner'])) {
                 $twig->addExtension(new DumpExtension($app['var_dumper.cloner']));
             }
         }
         return $twig;
     };
     $app['twig.loader.filesystem'] = function ($app) {
         return new \Twig_Loader_Filesystem($app['twig.path']);
     };
     $app['twig.loader.array'] = function ($app) {
         return new \Twig_Loader_Array($app['twig.templates']);
     };
     $app['twig.loader'] = function ($app) {
         return new \Twig_Loader_Chain(array($app['twig.loader.array'], $app['twig.loader.filesystem']));
     };
     $app['twig.environment_factory'] = $app->protect(function ($app) {
         return new \Twig_Environment($app['twig.loader'], $app['twig.options']);
     });
 }
 protected function setRequestStack($request)
 {
     $requestStackMock = $this->getMock('Symfony\\Component\\HttpFoundation\\RequestStack');
     $requestStackMock->method('getCurrentRequest')->willReturn($request);
     $this->appVariable->setRequestStack($requestStackMock);
 }
 /**
  * @param Container $app
  */
 public function register(Container $app)
 {
     // only register if twig templates are enabled by the framework
     if ($this->config['view.twig.enabled']) {
         $app['twig.settings'] = $settings = $this->config['view.twig.defaults'];
         $app['twig.options'] = $this->config['view.twig.defaults.options'];
         $app['twig.template.paths'] = $app['twig.settings']['filesystem'];
         $app['twig.form.templates'] = $app['twig.settings']['form']['templates'];
         # 'form_div_layout.html.twig'];
         $app['twig.templates'] = $app['twig.settings']['templates'];
         $app->register(new TranslationServiceProvider(), ['locale_fallbacks' => ['en']]);
         $app['twig.app_variable'] = function ($app) {
             $var = new AppVariable();
             if (isset($app['security.token_storage'])) {
                 $var->setTokenStorage($app['security.token_storage']);
             }
             if (isset($app['request_stack'])) {
                 $var->setRequestStack($app['request_stack']);
             }
             $var->setDebug($app['debug']);
             return $var;
         };
         $app['twig.finder'] = $app->protect(function ($template) use($app) {
             // try locating the
             $finder = new Finder();
             // add twig template paths
             foreach ($app['twig.template.paths'] as $dir) {
                 $finder->in(rtrim($dir, '/'));
             }
             // look for a match
             /** @var \SplFileInfo $file */
             foreach ($finder->files() as $file) {
                 if ($file->getFilename() === $template) {
                     return TRUE;
                 }
             }
             return FALSE;
         });
         $app['twig.loader.filesystem'] = function ($app) {
             return new \Twig_Loader_Filesystem($app['twig.template.paths']);
         };
         $app['twig.loader.array'] = function ($app) {
             return new \Twig_Loader_Array($app['twig.templates']);
         };
         $app['twig.loader'] = function ($app) {
             return new \Twig_Loader_Chain([$app['twig.loader.array'], $app['twig.loader.filesystem']]);
         };
         $app['twig.environment_factory'] = $app->protect(function ($app) {
             return new \Twig_Environment($app['twig.loader'], $app['twig.options']);
         });
         $app['twig'] = function ($app) {
             /** @var \Twig_Environment $twig */
             $twig = $app['twig.environment_factory']($app);
             // registered for BC, but should not be used anymore
             // deprecated and should probably be removed in Silex 3.0
             $twig->addGlobal('app', $app);
             if ($app['debug']) {
                 $twig->addExtension(new \Twig_Extension_Debug());
             }
             if (class_exists('Symfony\\Bridge\\Twig\\Extension\\RoutingExtension')) {
                 $twig->addGlobal('global', $app['twig.app_variable']);
                 if (isset($app['request_stack'])) {
                     $twig->addExtension(new HttpFoundationExtension($app['request_stack']));
                     $twig->addExtension(new RoutingExtension($app['url_generator']));
                 }
                 if (isset($app['translator'])) {
                     $twig->addExtension(new TranslationExtension($app['translator']));
                 }
                 if (isset($app['security.authorization_checker'])) {
                     $twig->addExtension(new SecurityExtension($app['security.authorization_checker']));
                 }
                 if (isset($app['fragment.handler'])) {
                     /** @type HIncludeFragmentRenderer */
                     $app['fragment.renderer.hinclude']->{'setTemplating'}($twig);
                     $twig->addExtension(new HttpKernelExtension($app['fragment.handler']));
                 }
                 if (isset($app['assets.packages'])) {
                     $twig->addExtension(new AssetExtension($app['assets.packages']));
                 }
                 if (isset($app['form.factory'])) {
                     $app['twig.form.engine'] = function ($app) {
                         return new TwigRendererEngine($app['twig.form.templates']);
                     };
                     $app['twig.form.renderer'] = function ($app) {
                         return new TwigRenderer($app['twig.form.engine'], $app['csrf.token_manager']);
                     };
                     $twig->addExtension(new FormExtension($app['twig.form.renderer']));
                     // add loader for Symfony built-in form templates
                     $reflected = new \ReflectionClass('Symfony\\Bridge\\Twig\\Extension\\FormExtension');
                     $path = dirname($reflected->getFileName()) . '/../Resources/views/Form';
                     /** @var \Twig_Loader_Chain */
                     $app['twig.loader']->{'addLoader'}(new \Twig_Loader_Filesystem($path));
                 }
                 if (isset($app['var_dumper.cloner'])) {
                     $twig->addExtension(new DumpExtension($app['var_dumper.cloner']));
                 }
             }
             return $twig;
         };
     }
 }