/** * @param ContainerInterface $container * @return TwigRenderer */ public function __invoke(ContainerInterface $container) { $config = $container->has('config') ? $container->get('config') : []; $debug = array_key_exists('debug', $config) ? (bool) $config['debug'] : false; $config = isset($config['templates']) ? $config['templates'] : []; $cacheDir = isset($config['cache_dir']) ? $config['cache_dir'] : false; // Create the engine instance $loader = new TwigLoader(); $environment = new TwigEnvironment($loader, ['cache' => $debug ? false : $cacheDir, 'debug' => $debug, 'strict_variables' => $debug, 'auto_reload' => $debug]); // Add extensions if ($container->has(RouterInterface::class)) { $environment->addExtension(new TwigExtension($container->get(RouterInterface::class), isset($config['assets_url']) ? $config['assets_url'] : '', isset($config['assets_version']) ? $config['assets_version'] : '')); } if ($debug) { $environment->addExtension(new TwigExtensionDebug()); } // Inject environment $twig = new TwigRenderer($environment, isset($config['extension']) ? $config['extension'] : 'html.twig'); // Add template paths $allPaths = isset($config['paths']) && is_array($config['paths']) ? $config['paths'] : []; foreach ($allPaths as $namespace => $paths) { $namespace = is_numeric($namespace) ? null : $namespace; foreach ((array) $paths as $path) { $twig->addPath($path, $namespace); } } return $twig; }
/** * @param ContainerInterface $container * @return TwigRenderer * @throws Exception\InvalidConfigException for invalid config service values. */ public function __invoke(ContainerInterface $container) { $config = $container->has('config') ? $container->get('config') : []; if (!is_array($config) && !$config instanceof ArrayObject) { throw new Exception\InvalidConfigException(sprintf('"config" service must be an array or ArrayObject for the %s to be able to consume it; received %s', __CLASS__, is_object($config) ? get_class($config) : gettype($config))); } $debug = array_key_exists('debug', $config) ? (bool) $config['debug'] : false; $config = $this->mergeConfig($config); $cacheDir = isset($config['cache_dir']) ? $config['cache_dir'] : false; // Create the engine instance $loader = new TwigLoader(); $environment = new TwigEnvironment($loader, ['cache' => $debug ? false : $cacheDir, 'debug' => $debug, 'strict_variables' => $debug, 'auto_reload' => $debug]); // Add expressive twig extension if ($container->has(ServerUrlHelper::class) && $container->has(UrlHelper::class)) { $environment->addExtension(new TwigExtension($container->get(ServerUrlHelper::class), $container->get(UrlHelper::class), isset($config['assets_url']) ? $config['assets_url'] : '', isset($config['assets_version']) ? $config['assets_version'] : '', isset($config['globals']) ? $config['globals'] : [])); } // Add debug extension if ($debug) { $environment->addExtension(new TwigExtensionDebug()); } // Add user defined extensions $extensions = isset($config['extensions']) && is_array($config['extensions']) ? $config['extensions'] : []; $this->injectExtensions($environment, $container, $extensions); // Inject environment $twig = new TwigRenderer($environment, isset($config['extension']) ? $config['extension'] : 'html.twig'); // Add template paths $allPaths = isset($config['paths']) && is_array($config['paths']) ? $config['paths'] : []; foreach ($allPaths as $namespace => $paths) { $namespace = is_numeric($namespace) ? null : $namespace; foreach ((array) $paths as $path) { $twig->addPath($path, $namespace); } } return $twig; }