/** * Initialise the extension. * * @param ExtensionInterface $extension * @param string $name * * @throws \Exception */ private function loadExtensionInitialize(ExtensionInterface $extension, $name) { try { $extension->initialize(); // Add an object of this extension to the global Twig scope. $namespace = $this->getNamespace($extension); if (!empty($namespace)) { $this->app['twig'] = $this->app->share($this->app->extend('twig', function (\Twig_Environment $twig) use($namespace, $extension) { $twig->addGlobal($namespace, $extension); return $twig; })); } } catch (\Exception $e) { $this->logInitFailure('Initialisation failed', $name, $e, Logger::ERROR); throw $e; } }
/** * Initialise the extension. * * @param ExtensionInterface $extension * @param string $name * * @throws \Exception */ private function loadExtensionInitialize(ExtensionInterface $extension, $name) { try { $extension->initialize(); } catch (\Exception $e) { $this->logInitFailure('Initialisation failed', $name, $e, Logger::ERROR); throw $e; } }
protected function initializeExtension(ExtensionInterface $extension) { $name = $extension->getName(); // Attempt to get extension YAML config try { $extension->getConfig(); } catch (\Exception $e) { $this->logInitFailure('Failed to load YAML config', $name, $e, Logger::ERROR); return; } // Call extension initialize() try { $extension->initialize(); // Add an object of this extension to the global Twig scope. $namespace = $this->getNamespace($extension); if (!empty($namespace)) { $this->app['twig'] = $this->app->share($this->app->extend('twig', function (\Twig_Environment $twig) use($namespace, $extension) { $twig->addGlobal($namespace, $extension); return $twig; })); } } catch (\Exception $e) { $this->logInitFailure('Initialisation failed', $name, $e, Logger::ERROR); return; } // Flag the extension as initialised $this->initialized[$name] = $extension; // If an extension makes it known it sends email, increase the counter if (is_callable(array($extension, 'sendsMail')) && $extension->sendsMail()) { $this->mailsenders++; } // Get the extension defined snippets try { $this->getSnippets($name); } catch (\Exception $e) { $this->logInitFailure('Snippet loading failed', $name, $e, Logger::ERROR); return; } // Add Twig extensions if (!is_callable(array($extension, 'getTwigExtensions'))) { return; } /** @var \Twig_Extension[] $extensions */ $twigExtensions = $extension->getTwigExtensions(); $addTwigExFunc = array($this, 'addTwigExtension'); foreach ($twigExtensions as $twigExtension) { $this->app['twig'] = $this->app->share($this->app->extend('twig', function (\Twig_Environment $twig) use($addTwigExFunc, $twigExtension, $name) { call_user_func($addTwigExFunc, $twig, $twigExtension, $name); return $twig; })); if (!is_callable(array($extension, 'isSafe')) || !$extension->isSafe()) { continue; } $this->app['safe_twig'] = $this->app->share($this->app->extend('safe_twig', function (\Twig_Environment $twig) use($addTwigExFunc, $twigExtension, $name) { call_user_func($addTwigExFunc, $twig, $twigExtension, $name); return $twig; })); } }