Exemplo n.º 1
0
 public function init()
 {
     $view_dir = [__DIR__ . '/../Views', __DIR__ . '/../Templates'];
     $twigConfig = [];
     if ($this->config['twig']['cache']) {
         $twigConfig["cache"] = $this->app['cache']['twig'];
     }
     $twigConfig["debug"] = $this->config['twig']['debug'];
     $loader = new \Twig_Loader_Filesystem($view_dir);
     foreach ($view_dir as $d) {
         $loader->addPath($d, 'Meister');
     }
     foreach ($this->config['modules'] as $app) {
         if (file_exists($this->app['Modules'] . $app . '/Views')) {
             $loader->addPath($this->app['Modules'] . $app . '/Views', $app);
         }
         if (file_exists($this->app['Modules'] . $app . '/Templates')) {
             $loader->addPath($this->app['Modules'] . $app . '/Templates', $app);
         }
     }
     $this->twig = new \Twig_Environment($loader, $twigConfig);
     $this->twig->addExtension(new \Twig_Extensions_Extension_I18n());
     /**
      * Verifica permissões para exibir determinada coisa
      */
     $function = new \Twig_SimpleFunction('permission', function ($rule) {
         return $this->app['auth']->checkRules($rule);
     });
     $this->twig->addFunction($function);
 }
Exemplo n.º 2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('<comment>Iniciando busca de templates</comment>');
     /**
      * @var $app \app\AppInit
      */
     $app = $this->getHelper('init')->getInit();
     $cache = $app->getCache();
     $src = $app->getBaseDir() . "/src";
     $loader = new \Twig_Loader_Filesystem($src);
     $twig = new \Twig_Environment($loader, array('cache' => $cache['twig'], 'auto_reload' => true));
     $config = $app->config();
     $container = $app->container();
     foreach ($config['modules'] as $ap) {
         if (file_exists($container['Modules'] . $ap . '/Views')) {
             $loader->addPath($container['Modules'] . $ap . '/Views', $ap);
         }
         if (file_exists($container['Modules'] . $ap . '/Templates')) {
             $loader->addPath($container['Modules'] . $ap . '/Templates', $ap);
         }
     }
     $twig->addExtension(new \Twig_Extensions_Extension_I18n());
     $d = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($src), \RecursiveIteratorIterator::LEAVES_ONLY);
     $Regex2 = new \RegexIterator($d, '/\\.html.twig$/i');
     foreach ($Regex2 as $file) {
         if ($file->isFile()) {
             $twig->loadTemplate(str_replace($src . '/', '', $file));
         }
     }
     #$output->writeln("<comment>Finalizando</comment>");
 }
Exemplo n.º 3
0
 public static function paths()
 {
     $loader = new Twig_Loader_Filesystem(TEMPLATES);
     $loader->addPath(TEMPLATES . "module/user/", 'ModuleUser');
     $loader->addPath(TEMPLATES . "module/login/", 'ModuleLogin');
     $loader->addPath(TEMPLATES . "modales/", 'Modal');
     return $loader;
 }
Exemplo n.º 4
0
 private function createLoader($templatePath)
 {
     $loader = new \Twig_Loader_Filesystem($templatePath);
     $loader->addPath($_SERVER["DOCUMENT_ROOT"] . SITE_TEMPLATE_PATH . "/", "templateRoot");
     $loader->addPath($_SERVER["DOCUMENT_ROOT"] . SITE_TEMPLATE_PATH . "/template/", "template");
     $loader->addPath($_SERVER["DOCUMENT_ROOT"] . SITE_TEMPLATE_PATH . "/components/", "templateComponents");
     return $loader;
 }
Exemplo n.º 5
0
 private function createLoader($templatePath)
 {
     $loader = new \Twig_Loader_Filesystem($templatePath);
     $loader->addPath($templatePath . "/", "root");
     $loader->addPath($templatePath . "/components", "components");
     $loader->addPath($templatePath . "/template", "template");
     return $loader;
 }
Exemplo n.º 6
0
 /**
  * add a directory to template search dirs
  * @param string $directory
  * @param bool|true $primary
  * @return ITemplateEngine|void
  */
 public function addDir($directory, $primary = true)
 {
     if ($primary) {
         $this->twigLoader->prependPath($directory);
     } else {
         $this->twigLoader->addPath($directory);
     }
     $this->twigEnv->setLoader($this->twigLoader);
     return $this;
 }
Exemplo n.º 7
0
 public function __construct(Twig_Loader_Filesystem $loader, $path, $namespace = self::STORE_NAMESPACE)
 {
     $this->loader = $loader;
     $this->path = str_replace('\\', '/', rtrim($path, '/\\') . '/');
     $path = rtrim($this->path, '/\\');
     if (!file_exists($path)) {
         mkdir($path, 0777, true);
         chmod($path, 0777);
         // just for sure
     }
     $this->loader->addPath($this->path, $namespace);
 }
Exemplo n.º 8
0
 public function testLoadTemplateAndRenderBlockWithCache()
 {
     $loader = new Twig_Loader_Filesystem(array());
     $loader->addPath(dirname(__FILE__) . '/Fixtures/themes/theme2');
     $loader->addPath(dirname(__FILE__) . '/Fixtures/themes/theme1');
     $loader->addPath(dirname(__FILE__) . '/Fixtures/themes/theme1', 'default_theme');
     $twig = new Twig_Environment($loader);
     $template = $twig->loadTemplate('blocks.html.twig');
     $this->assertSame('block from theme 1', $template->renderBlock('b1', array()));
     $template = $twig->loadTemplate('blocks.html.twig');
     $this->assertSame('block from theme 2', $template->renderBlock('b2', array()));
 }
Exemplo n.º 9
0
 static function getLoader($request)
 {
     $loader = new \Twig_Loader_Filesystem();
     $project = Project::getCurrent();
     foreach ($project->getSetting('TEMPLATE_DIRS', []) as $dir) {
         $dir = realpath($dir);
         if (is_dir($dir)) {
             $loader->addPath($dir);
         }
     }
     $loader->addPath(dirname(__DIR__) . '/BuiltIn/');
     return $loader;
 }
Exemplo n.º 10
0
 public function initExtension($extension, \Twig_Environment $render, \Twig_Loader_Filesystem $fileSystemLoader)
 {
     if (false === strpos($extension, "\\")) {
         $extension = "\\" . $extension;
     }
     $config = $this->getConfig();
     switch ($extension) {
         case "\\TranslationExtension":
             $config = isset($config["translationExtension"]) ? $config["translationExtension"] : [];
             $lang = isset($config["lang"]) ? $config["lang"] : "ru";
             $locale = isset($config["locale"]) ? $config["locale"] : "ru_RU";
             $translator = new \Symfony\Component\Translation\Translator($locale);
             $translator->addLoader('xlf', new \Symfony\Component\Translation\Loader\XliffFileLoader());
             $vendorFormDir = VENDOR_DIR . '/symfony/form/Symfony/Component/Form';
             $vendorValidatorDir = VENDOR_DIR . '/symfony/validator/Symfony/Component/Validator';
             $translator->addResource('xlf', $vendorFormDir . "/Resources/translations/validators.{$lang}.xlf", $locale, 'validators');
             $translator->addResource('xlf', $vendorValidatorDir . "/Resources/translations/validators.{$lang}.xlf", $locale, 'validators');
             $extension = new \Symfony\Bridge\Twig\Extension\TranslationExtension($translator);
             break;
         case "\\FormExtension":
             $config = isset($config["formExtension"]) ? $config["formExtension"] : [];
             $templates = $config["templates"] ?: "/vendor/symfony/twig-bridge/Resources/views/Form";
             $templates = $this->getRootDir() . "/" . $templates;
             $fileSystemLoader->addPath($templates);
             $formTemplate = $config["formTheme"] ?: "form_div_layout.html.twig";
             $formTemplate = (array) $formTemplate;
             $formEngine = new \Symfony\Bridge\Twig\Form\TwigRendererEngine($formTemplate);
             $formEngine->setEnvironment($render);
             $extension = new \Symfony\Bridge\Twig\Extension\FormExtension(new \Symfony\Bridge\Twig\Form\TwigRenderer($formEngine, $this->getFormCsrfProvider()));
             break;
         default:
             $extension = new $extension();
     }
     return $extension;
 }
Exemplo n.º 11
0
 protected function getTwig()
 {
     $options = ['cache' => false, 'strict_variables' => true];
     $loader = new \Twig_Loader_Filesystem();
     $loader->addPath($this->skeletonDirs);
     return new \Twig_Environment($loader, $options);
 }
Exemplo n.º 12
0
 /**
  * Response constructor.
  * @param $view
  * @param array $param
  */
 public function __construct($view, $param = [])
 {
     $loader = new \Twig_Loader_Filesystem(SRC_ROUTE . "/Views");
     $loader->addPath(SRC_ROUTE . "/", "");
     $this->_twig = new \Twig_Environment($loader, ["charset" => "utf-8", "debug" => true]);
     echo $this->_twig->render($view, $param);
 }
Exemplo n.º 13
0
 public function testGetNamespaces()
 {
     $loader = new Twig_Loader_Filesystem(sys_get_temp_dir());
     $this->assertEquals(array(Twig_Loader_Filesystem::MAIN_NAMESPACE), $loader->getNamespaces());
     $loader->addPath(sys_get_temp_dir(), 'named');
     $this->assertEquals(array(Twig_Loader_Filesystem::MAIN_NAMESPACE, 'named'), $loader->getNamespaces());
 }
Exemplo n.º 14
0
 public function render($echo = false)
 {
     // Load template directories.
     $loader = new \Twig_Loader_Filesystem();
     $loader->addPath('templates');
     // Set up Twig.
     $twig = new \Twig_Environment($loader, array('debug' => true, 'strct_variables' => true));
     $twig->addExtension(new \Twig_Extension_Debug());
     // Mardown support.
     $twig->addFilter(new \Twig_SimpleFilter('markdown', function ($text) {
         $parsedown = new \Parsedown();
         return $parsedown->text($text);
     }));
     // DB queries.
     $twig->addFunction(new \Twig_SimpleFunction('db_queries', function () {
         return Db::getQueries();
     }));
     // Render.
     $string = $twig->render($this->template, $this->data);
     if ($echo) {
         echo $string;
     } else {
         return $string;
     }
 }
Exemplo n.º 15
0
 public static function getEnvironment()
 {
     if (!self::$environment) {
         $applicationReflector = new \ReflectionClass(Application::getInstance());
         $loader = new \Twig_Loader_Filesystem();
         self::$environment = new Twig_Environment($loader);
         // ppFramework built-in views
         $loader->addPath(__DIR__ . DIRECTORY_SEPARATOR . "View", "ppFramework");
         // Application view namespace
         $path = dirname($applicationReflector->getFileName()) . DIRECTORY_SEPARATOR . $applicationReflector->getShortName() . DIRECTORY_SEPARATOR . "View";
         if (is_dir($path)) {
             $loader->addPath($path, $applicationReflector->getShortName());
         }
     }
     return self::$environment;
 }
 /**
  * @throws \Twig_Error_Loader
  */
 protected function setUp()
 {
     // Setup factory for tabs
     $this->tabFactory = Forms::createFormFactory();
     parent::setUp();
     $rendererEngine = new TwigRendererEngine(array('form_div_layout.html.twig', 'fields.html.twig'));
     if (interface_exists('Symfony\\Component\\Security\\Csrf\\CsrfTokenManagerInterface')) {
         $csrfProviderInterface = 'Symfony\\Component\\Security\\Csrf\\CsrfTokenManagerInterface';
     } else {
         $csrfProviderInterface = 'Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\CsrfProviderInterface';
     }
     $renderer = new TwigRenderer($rendererEngine, $this->getMock($csrfProviderInterface));
     $this->extension = new FormExtension($renderer);
     $reflection = new \ReflectionClass($renderer);
     $bridgeDirectory = dirname($reflection->getFileName()) . '/../Resources/views/Form';
     $loader = new \Twig_Loader_Filesystem(array($bridgeDirectory, __DIR__ . '/../../Resources/views/Form'));
     $loader->addPath(__DIR__ . '/../../Resources/views', 'MopaBootstrap');
     $environment = new Twig_Environment($loader, array('strict_variables' => true));
     $environment->addExtension(new TranslationExtension(new StubTranslator()));
     $environment->addExtension(new IconExtension('fontawesome'));
     $environment->addExtension(new FormExtension2());
     $environment->addGlobal('global', '');
     $environment->addExtension($this->extension);
     $this->extension->initRuntime($environment);
 }
Exemplo n.º 17
0
 /**
  * {@inheritDoc}
  */
 public function createConfig(array $config = array())
 {
     $config = ArrayObject::ensureArrayObject($config);
     $config->defaults($this->defaultConfig);
     $config->defaults(array('payum.template.layout' => '@PayumCore/layout.html.twig', 'payum.http_client' => HttpClientFactory::create(), 'guzzle.client' => HttpClientFactory::createGuzzle(), 'twig.env' => function (ArrayObject $config) {
         $loader = new \Twig_Loader_Filesystem();
         foreach ($config['payum.paths'] as $namespace => $path) {
             $loader->addPath($path, $namespace);
         }
         return new \Twig_Environment($loader);
     }, 'payum.action.get_http_request' => new GetHttpRequestAction(), 'payum.action.capture_payment' => new CapturePaymentAction(), 'payum.action.execute_same_request_with_model_details' => new ExecuteSameRequestWithModelDetailsAction(), 'payum.action.render_template' => function (ArrayObject $config) {
         return new RenderTemplateAction($config['twig.env'], $config['payum.template.layout']);
     }, 'payum.extension.endless_cycle_detector' => new EndlessCycleDetectorExtension(), 'payum.action.get_currency' => function (ArrayObject $config) {
         return new GetCurrencyAction($config['payum.iso4217']);
     }, 'payum.prepend_actions' => array(), 'payum.prepend_extensions' => array(), 'payum.prepend_apis' => array(), 'payum.default_options' => array(), 'payum.required_options' => array(), 'payum.api.http_client' => function (ArrayObject $config) {
         return $config['payum.http_client'];
     }, 'payum.security.token_storage' => null));
     if ($config['payum.security.token_storage']) {
         $config['payum.action.get_token'] = function (ArrayObject $config) {
             return new GetTokenAction($config['payum.security.token_storage']);
         };
     }
     $config['payum.paths'] = array_replace(['PayumCore' => __DIR__ . '/Resources/views'], $config['payum.paths'] ?: []);
     return (array) $config;
 }
Exemplo n.º 18
0
 public function initRuntime(\Twig_Environment $environment)
 {
     $this->twigEnvironment = $environment;
     $loader = new \Twig_Loader_Filesystem();
     $loader->addPath(__DIR__ . '/../Resorces/views', 'np_navigation');
     $environment->getLoader()->addLoader($loader);
 }
Exemplo n.º 19
0
 public function testGetNamespaces()
 {
     $loader = new Twig_Loader_Filesystem(sys_get_temp_dir());
     $this->assertEquals(array('__main__'), $loader->getNamespaces());
     $loader->addPath(sys_get_temp_dir(), 'named');
     $this->assertEquals(array('__main__', 'named'), $loader->getNamespaces());
 }
Exemplo n.º 20
0
 public static function getInstance()
 {
     if (self::$instance !== null) {
         return self::$instance;
     }
     $loader = new \Twig_Loader_Filesystem();
     $translator = Translator::getInstance();
     $modules = \SimpleSAML_Module::getModules();
     foreach ($modules as $module) {
         if (\SimpleSAML_Module::isModuleEnabled($module)) {
             $path = \SimpleSAML_Module::getModuleDir($module);
             $templatePath = self::resourceExists('templates', $path);
             if (false !== $templatePath) {
                 $loader->addPath($templatePath, $module);
             }
             $translationPath = self::resourceExists('translations', $path);
             if (false !== $translationPath) {
                 $translations = new Finder();
                 $translations->files()->in($translationPath)->name('/\\.[a-zA-Z_]+\\.yml$/');
                 /** @var SplFileInfo $translation */
                 foreach ($translations as $translation) {
                     $name = $translation->getBasename('.yml');
                     $locale = substr($name, strrpos($name, '.') + 1);
                     $translator->addResource('yaml', $translation->getPathname(), $locale, $module);
                 }
             }
         }
     }
     self::$instance = new \Twig_Environment($loader);
     self::$instance->addExtension(new TranslationExtension($translator));
     return self::$instance;
 }
 /**
  * {@inheritdoc}
  */
 public function register(Container $app)
 {
     $app['twig.path'] = array($app['app.templates.path']);
     $app['twig.templates'] = array();
     $app['twig.loader'] = function () use($app) {
         $loaders = [];
         $twigLoaderFs = new \Twig_Loader_Filesystem($app['twig.path']);
         foreach ($app['extensions'] as $info) {
             if (!is_dir($templateViewDirectory = $info['pathName'] . '/' . self::EXTENSION_TEMPLATE_PATH)) {
                 throw new InvalidTemplateDirectoryException(sprintf('"%s" is not a directory', $templateViewDirectory));
             }
             $currentController = $app['request']->get('_controller');
             if (strstr($currentController, '\\', true) === $info['name']) {
                 $twigLoaderFs->addPath($templateViewDirectory);
                 break;
             }
         }
         $loaders[] = $twigLoaderFs;
         $loaders[] = new \Twig_Loader_Array($app['twig.templates']);
         return new \Twig_Loader_Chain($loaders);
     };
     $app['twig.environment'] = function () use($app) {
         $isTemplateMustBeCached = $app['twig.cache_templates'];
         $templateCacheDirectory = $app['twig.cache.directory'];
         $options = [];
         if ($isTemplateMustBeCached && $this->isTemplateCacheDirectoryValid($templateCacheDirectory)) {
             $options = ['cache' => $templateCacheDirectory];
         }
         return new \Twig_Environment($app['twig.loader'], $options);
     };
     $app['twig'] = function () use($app) {
         return $app['twig.environment'];
     };
 }
Exemplo n.º 22
0
 /**
  * @return \Twig_Environment
  */
 public static function createGeneric()
 {
     $loader = new \Twig_Loader_Filesystem();
     foreach (static::createGenericPaths() as $path => $namespace) {
         $loader->addPath($path, $namespace);
     }
     return new \Twig_Environment($loader);
 }
Exemplo n.º 23
0
 /** @Provides("Twig_LoaderInterface") */
 static function provideLoader(array $namespaces = [], array $paths = [])
 {
     $loader = new \Twig_Loader_Filesystem("/");
     foreach (array_combine($namespaces, $paths) as $ns => $path) {
         $loader->addPath($path, $ns);
     }
     return $loader;
 }
Exemplo n.º 24
0
 /**
  * Adds a path to the template environment.
  *
  * @param $path
  * @throws InvalidPathException
  */
 public function addPath($path)
 {
     try {
         $this->twigFileLoader->addPath($path);
     } catch (\Twig_Error_Loader $e) {
         throw new InvalidPathException($e->getMessage());
     }
 }
Exemplo n.º 25
0
 /**
  * Add a templates directory to the end of the list of existing ones known by the current Twig templates loader.
  * @param string $path Path to the templates directory to add.
  * @param string $namespace Namespace used to reference the directory inside the Twig templates loader.
  */
 public function addTemplatesDirectory($path, $namespace = false)
 {
     if (StringUtils::emptyOrSpaces($namespace)) {
         $this->_loader->addPath($path);
     } else {
         $this->_loader->addPath($path, trim($namespace));
     }
 }
Exemplo n.º 26
0
 /**
  * TwigFilesystemProvider constructor.
  * @param \Twig_Loader_Filesystem $loader
  * @param string $root_path
  * @param string $namespace
  */
 public function __construct(\Twig_Loader_Filesystem $loader, $root_path, $namespace)
 {
     $this->loader = $loader;
     $this->namespace = $namespace;
     if (!in_array($namespace, $loader->getNamespaces())) {
         $loader->addPath($root_path, $namespace);
     }
 }
Exemplo n.º 27
0
 public function register(Container $app)
 {
     $app['twig.options'] = array('autoescape' => false, 'charset' => $app['app.charset'], 'debug' => $app['app.debug'], 'strict_variables' => $app['app.debug']);
     $app['twig.loader'] = function () use($app) {
         $theme = ucfirst($app->edition('theme'));
         $format = Toolkit::camelize($app->edition('format'), true);
         $loader = new \Twig_Loader_Filesystem($app['app.dir.themes']);
         // Base theme (common styles per edition type)
         // <easybook>/app/Resources/Themes/Base/<edition-type>/Templates/<template-name>.twig
         $baseThemeDir = sprintf('%s/Base/%s/Templates', $app['app.dir.themes'], $format);
         $loader->addPath($baseThemeDir);
         $loader->addPath($baseThemeDir, 'theme');
         $loader->addPath($baseThemeDir, 'theme_base');
         // Book theme (configured per edition in 'config.yml')
         // <easybook>/app/Resources/Themes/<theme>/<edition-type>/Templates/<template-name>.twig
         $bookThemeDir = sprintf('%s/%s/%s/Templates', $app['app.dir.themes'], $theme, $format);
         $loader->prependPath($bookThemeDir);
         $loader->prependPath($bookThemeDir, 'theme');
         $userTemplatePaths = array($app['publishing.dir.templates'], sprintf('%s/%s', $app['publishing.dir.templates'], strtolower($format)), sprintf('%s/%s', $app['publishing.dir.templates'], $app['publishing.edition']));
         foreach ($userTemplatePaths as $path) {
             if (file_exists($path)) {
                 $loader->prependPath($path);
             }
         }
         $defaultContentPaths = array(sprintf('%s/Base/%s/Contents', $app['app.dir.themes'], $format), sprintf('%s/%s/%s/Contents', $app['app.dir.themes'], $theme, $format));
         foreach ($defaultContentPaths as $path) {
             if (file_exists($path)) {
                 $loader->prependPath($path, 'content');
             }
         }
         return $loader;
     };
     $app['twig'] = function () use($app) {
         $twig = new \Twig_Environment($app['twig.loader'], $app['twig.options']);
         $twig->addExtension(new TwigCssExtension());
         $twig->addGlobal('app', $app);
         if (null !== ($bookConfig = $app['publishing.book.config'])) {
             $twig->addGlobal('book', $bookConfig['book']);
             $publishingEdition = $app['publishing.edition'];
             $editions = $app->book('editions');
             $twig->addGlobal('edition', $editions[$publishingEdition]);
         }
         return $twig;
     };
 }
Exemplo n.º 28
0
 protected function createRenderer()
 {
     $config = $this->container['app.config'];
     $loader = new \Twig_Loader_Filesystem($this->app->getTemplateDir());
     $twig = new \Twig_Environment($loader, ['debug' => $config->get('debug', false), 'cache' => $config->get('twig.cache', false), 'charset' => $config->get('twig.charset', 'utf-8')]);
     // add built-in template path
     $loader->addPath(__DIR__ . '/template/error');
     // add built-in extension
     $twig->addExtension((new DietcubeExtension())->setContainer($this->container));
     if ($this->app->isDebug()) {
         // add built-in debug template path
         $twig->addExtension(new \Twig_Extension_Debug());
         $loader->addPath(__DIR__ . '/template/debug', 'debug');
     }
     $twig->addGlobal('query', $this->container['global.get']->getData());
     $twig->addGlobal('body', $this->container['global.post']->getData());
     return $twig;
 }
Exemplo n.º 29
0
 /**
  * @return \Twig_Environment
  */
 public static function createGeneric()
 {
     $paths = array_filter(array('PayumCore' => self::guessViewsPath('Payum\\Core\\Payment'), 'PayumStripe' => self::guessViewsPath('Payum\\Stripe\\PaymentFactory'), 'PayumKlarnaCheckout' => self::guessViewsPath('Payum\\Klarna\\Checkout\\PaymentFactory')));
     $loader = new \Twig_Loader_Filesystem();
     foreach ($paths as $namespace => $path) {
         $loader->addPath($path, $namespace);
     }
     return new \Twig_Environment($loader);
 }
Exemplo n.º 30
0
 /**
  * Set Template dir
  *
  * @access public
  * @param string $directory
  */
 public function add_template_directory($directory, $namespace = null)
 {
     if ($this->filesystem_loader === null) {
         $this->filesystem_loader = new \Twig_Loader_Filesystem($directory);
     }
     if ($namespace === null) {
         $this->filesystem_loader->addPath($directory);
     } else {
         $this->filesystem_loader->addPath($directory, $namespace);
     }
 }