getGlobals() public method

Gets the registered Globals.
public getGlobals ( ) : array
return array An array of globals
 /**
  * @param GetResponseEvent $event
  */
 public function onKernelRequest(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     $host = $request->getHost();
     $baseHost = $this->baseHost;
     $subdomain = str_replace('.' . $baseHost, '', $host);
     //Check subDomain
     $this->checkOldDomains($subdomain);
     //Fix logout bug
     $str = $baseHost . "/login";
     if ($host != $baseHost && strstr($request->getUri(), $str, true)) {
         $event->setResponse(new RedirectResponse($this->router->generate('buddy_system_user_homepage_index')));
     }
     //Fix dashboard error
     if ($this->security_context->getToken() && $this->security_context->isGranted('IS_AUTHENTICATED_REMEMBERED') && $request->get('_route') == 'buddy_system_user_homepage_index') {
         $this->checkSectionAccess();
         $this->activityManager->setUser($this->security_context);
         $this->activityManager->login();
         if ($this->security_context->isGranted('ROLE_ADMIN') || $this->security_context->isGranted('ROLE_SUPER_ADMIN')) {
             $event->setResponse(new RedirectResponse($this->router->generate('buddy_system_sadmin_homepage')));
         } else {
             if ($this->security_context->isGranted('ROLE_BUDDYCOORDINATOR')) {
                 $event->setResponse(new RedirectResponse($this->router->generate('buddy_system_admin_homepage')));
             } else {
                 $event->setResponse(new RedirectResponse($this->router->generate('buddy_system_members_homepage')));
             }
         }
     }
     if ($host == $baseHost) {
         if ($request->get('_route') != null && $request->get('_route') != "buddy_system_choose" && $request->get('_route') != "buddy_system_front_change_language_ajax") {
             $event->setResponse(new RedirectResponse($this->router->generate('buddy_system_choose')));
         }
     } else {
         //Redirection when /en or /fr at the end
         $url = $request->getUri();
         if (substr($url, -3) == "/fr" || substr($url, -3) == "/en") {
             $event->setResponse(new RedirectResponse(substr($url, 0, strlen($url) - 3)));
         }
         //Add Section to local
         if (!$this->sectionManager->getCurrentSection()) {
             /** @var Section $section */
             $section = $this->em->getRepository('BuddySystemMainBundle:Section')->findOneBy(array('subdomain' => $subdomain));
             //Fix error on www
             if (!$section && $subdomain == "www") {
                 header('Location: http://buddysystem.eu');
             }
             if (!$section) {
                 throw new NotFoundHttpException(sprintf('Cannot find section for host "%s", subdomain "%s"', $host, $subdomain));
             }
             if (!array_key_exists('section', $this->twig->getGlobals())) {
                 $this->twig->addGlobal('section', $section);
             }
             $this->sectionManager->setCurrentSection($section);
         }
     }
     if ($this->security_context->getToken() && $this->security_context->getToken()->getUser() && $this->sectionManager->getCurrentSection()) {
         $this->checkSectionAccess();
     }
 }
 function let(\Twig_Environment $environment, AssetsExtension $assets, FSiFilePathResolver $filePathResolver)
 {
     $this->beConstructedWith($filePathResolver);
     $environment->hasExtension('assets')->shouldBeCalled()->willReturn(true);
     $environment->getExtension('assets')->shouldBeCalled()->willReturn($assets);
     $environment->getGlobals()->shouldBeCalled()->willReturn(array());
     $this->initRuntime($environment);
 }
 public function testAddExtension()
 {
     $apiKey = '1234567';
     $twig = new \Twig_Environment($this->getMock('Twig_LoaderInterface'));
     $twig->addExtension(new RaygunSetupExtension($apiKey));
     $globals = $twig->getGlobals();
     $this->assertArrayHasKey('raygun_api_key', $globals);
     $this->assertEquals($apiKey, $globals['raygun_api_key']);
 }
 /**
  * Render block
  *
  * @param $name string
  * @param $parameters string
  * @return string
  */
 protected function renderBlock($name, $parameters)
 {
     foreach ($this->getTemplates() as $template) {
         if ($template->hasBlock($name)) {
             return $template->renderBlock($name, array_merge($this->environment->getGlobals(), $parameters, $this->params));
         }
     }
     throw new \InvalidArgumentException(sprintf('Block "%s" doesn\'t exist in grid template "%s".', $name, $this->theme));
 }
 /**
  * Forwards pathInfo to subrequests.
  * Allows HostPathSiteSelector to work.
  *
  * @param string $controller
  * @param array  $attributes
  * @param array  $query
  *
  * @return ControllerReference
  */
 public function controller($controller, $attributes = array(), $query = array())
 {
     $globals = $this->environment->getGlobals();
     if (!isset($attributes['pathInfo'])) {
         $sitePath = $this->siteSelector->retrieve()->getRelativePath();
         $currentPathInfo = $globals['app']->getRequest()->getPathInfo();
         $attributes['pathInfo'] = $sitePath . $currentPathInfo;
     }
     return $this->httpKernelExtension->controller($controller, $attributes, $query);
 }
 /**
  * Add app_dev.php in dev environment if the link is locale.
  *
  * @param \Twig_Environment $twig
  * @param $link
  *
  * @return string
  */
 public function menuLink(\Twig_Environment $twig, $link)
 {
     $globals = $twig->getGlobals();
     $app = $globals['app'];
     $env = $app->getDebug();
     if ($env && strpos($link, 'http') === false) {
         $dev = $link[0] === '/' ? 'app_dev.php' : 'app_dev.php/';
         $link = $dev . $link;
     }
     return $link;
 }
 /**
  * Render an action component
  *
  * @param  object $action    action
  * @param  string $component Component to render (subject, verb, etc ...)
  * @param  array  $variables Additional variables to pass to templates
  * @return string
  */
 public function renderActionComponent($action, $component, array $variables = array())
 {
     $action = $this->resolveAction($action, __METHOD__);
     if (null === $this->template) {
         $this->template = reset($this->resources);
         if (!$this->template instanceof \Twig_Template) {
             $this->template = $this->twig->loadTemplate($this->template);
         }
     }
     $componentVariables = $this->getComponentVariables($action, $component);
     $componentVariables['type'] = $component;
     $componentVariables['action'] = $action;
     $custom = false;
     if (!empty($componentVariables['model'])) {
         $custom = '_' . $componentVariables['normalized_model'];
     }
     $rendering = $custom . '_' . $component . 'component';
     $blocks = $this->getBlocks($action);
     if (isset($this->varStack[$rendering])) {
         $typeIndex = $this->varStack[$rendering]['typeIndex'] - 1;
         $types = $this->varStack[$rendering]['types'];
         $this->varStack[$rendering]['variables'] = array_replace_recursive($componentVariables, $variables);
     } else {
         $types = array();
         // fallback to __toString of component.
         if ($component != 'action') {
             $types[] = 'action';
         }
         $types[] = $component;
         if ($custom) {
             $types[] = $custom . '_default';
             $types[] = $custom . '_' . $component;
         }
         $typeIndex = count($types) - 1;
         $this->varStack[$rendering] = array('variables' => array_replace_recursive($componentVariables, $variables), 'types' => $types);
     }
     $twigGlobals = $this->twig->getGlobals();
     do {
         $types[$typeIndex] .= '_component';
         if (isset($blocks[$types[$typeIndex]])) {
             $this->varStack[$rendering]['typeIndex'] = $typeIndex;
             $context = array_merge($twigGlobals, $this->varStack[$rendering]['variables']);
             // we do not call renderBlock here to avoid too many nested level calls (XDebug limits the level to 100 by default)
             ob_start();
             $this->template->displayBlock($types[$typeIndex], $context, $blocks);
             $html = ob_get_clean();
             unset($this->varStack[$rendering]);
             return $html;
         }
     } while (--$typeIndex >= 0);
     throw new \Exception(sprintf('Unable to render the action component as none of the following blocks exist: "%s".', implode('", "', array_reverse($types))));
 }
 public static function locateSrc(\Twig_Environment $env, $id, $name, $type, $variant = NULL, $fallbackToDefaultVariant = true, $options = array())
 {
     $globals = $env->getGlobals();
     try {
         $url = $globals['MediaStorage_instance']->locate($id, $name, $type, $variant, $fallbackToDefaultVariant, $options);
         return $url;
     } catch (CannotLocateMediaException $e) {
         if ($globals['MediaStorage_instance_debug']) {
             throw $e;
         }
     }
     return "";
 }
Example #9
0
 /**
  * @param string $menuIdentifier
  * @param int    $depth
  *
  * @return string
  */
 public function renderMenu($menuIdentifier, $depth = 1, $type = 'main')
 {
     $queryBuilder = $this->repo->getNodesHierarchyQueryBuilder(null, false, array(), true)->join('ClasticMenuBundle:Menu', 'menu', 'menu.id = node.menu')->andWhere('menu.identifier = :identifier')->setParameter('identifier', $menuIdentifier);
     $globals = $this->environment->getGlobals();
     /** @var GlobalVariables $variables */
     $variables = $globals['app'];
     $currentUrl = $variables->getRequest()->server->get('REQUEST_URI');
     $items = array_map(function (MenuItem $item) use($currentUrl) {
         $url = $item->getUrl();
         $node = $item->getNode();
         // TODO Remove getTitle once a solution is found.
         if ($node && $node->getTitle() && isset($node->alias)) {
             $url = '/' . $node->alias->getAlias();
         }
         return array('title' => $item->getTitle(), 'left' => $item->getLeft(), 'right' => $item->getRight(), 'root' => $item->getRoot(), 'level' => $item->getLevel(), 'id' => $item->getId(), '_node' => $item, '_active' => $url == $currentUrl, '_link' => $url);
     }, $queryBuilder->getQuery()->getResult());
     $template = 'AppBundle:Twig:menu.html.twig';
     if ($type == 'footer') {
         $template = 'AppBundle:Twig:menu_footer.html.twig';
     }
     return $this->environment->render($template, array('tree' => $this->repo->buildTree($items)));
 }
 public function render(\Twig_Environment $twig, $uri)
 {
     $globals = $twig->getGlobals();
     $request = $globals['app']['request'];
     $subRequest = Request::create($uri, 'get', array(), $request->cookies->all(), array(), $request->server->all());
     if ($request->getSession()) {
         $subRequest->setSession($request->getSession());
     }
     $response = $globals['app']->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
     if (!$response->isSuccessful()) {
         throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
     }
     return $response->getContent();
 }
Example #11
0
 public function testRemoveExtension()
 {
     $twig = new Twig_Environment(new Twig_Loader_String());
     $twig->addExtension(new Twig_Tests_EnvironmentTest_Extension());
     $twig->removeExtension('test');
     $this->assertFalse(array_key_exists('test', $twig->getTags()));
     $this->assertFalse(array_key_exists('foo_filter', $twig->getFilters()));
     $this->assertFalse(array_key_exists('foo_function', $twig->getFunctions()));
     $this->assertFalse(array_key_exists('foo_test', $twig->getTests()));
     $this->assertFalse(array_key_exists('foo_unary', $twig->getUnaryOperators()));
     $this->assertFalse(array_key_exists('foo_binary', $twig->getBinaryOperators()));
     $this->assertFalse(array_key_exists('foo_global', $twig->getGlobals()));
     $this->assertCount(2, $twig->getNodeVisitors());
 }
Example #12
0
 public function actions(\Twig_Environment $twig, $entityCode, $action = 'list', $entity = false)
 {
     $globals = $twig->getGlobals();
     $tplPath = false;
     $globalTemplatesPath = $globals['kernel_root_dir'] . '/Resources/views/';
     $templates = ['backend/entity/actions/' . $entityCode . '/' . $action . '.html.twig', 'backend/entity/actions/default/' . $action . '.html.twig'];
     foreach ($templates as $template) {
         if (!file_exists($globalTemplatesPath . $template)) {
             continue;
         }
         $tplPath = $template;
         break;
     }
     return $twig->render($tplPath, ['entityCode' => $entityCode, 'entity' => $entity]);
 }
Example #13
0
function twig_deficon(Twig_Environment $env, $val, $def = '')
{
    $globals = $env->getGlobals();
    if (is_array($val)) {
        for ($i = 0; $i < count($val); $i++) {
            $ret[$i] = $val[$i];
            if ($ret[$i] == '') {
                $ret[$i] = $def;
            }
            if (substr($ret[$i], -4, 4) == ".png" || substr($ret[$i], -4, 4) == ".svg" and strpos($ret[$i], "/") === false) {
                $ret[$i] = $globals["icon0"] . $ret[$i];
            }
        }
    } else {
        $ret = $val;
        if ($ret == '') {
            $ret = $def;
        }
        if (substr($ret, -4, 4) == ".png" || substr($ret, -4, 4) == ".svg" and strpos($ret, "/") === false) {
            $ret = $globals["icon0"] . $ret;
        }
    }
    return $ret;
}
Example #14
0
 /**
  * Output all (relevant) fields to the browser. Convenient for dumping the
  * content in order in, say, a `record.twig` template, without having to
  * iterate over them in the browser.
  *
  * @param \Twig_Environment $env
  * @param \Bolt\Legacy\Content $record
  * @param bool                 $common
  * @param bool                 $extended
  * @param bool                 $repeaters
  * @param string               $template
  * @param string|array         $exclude
  *
  * @return string
  */
 public function fields(\Twig_Environment $env, $record = null, $common = true, $extended = false, $repeaters = true, $template = '_sub_fields.twig', $exclude = null)
 {
     // If $record is empty, we must get it from the global scope in Twig.
     if (!$record instanceof \Bolt\Legacy\Content) {
         $globals = $env->getGlobals();
         $record = $globals['record'];
     }
     // Still no record? Nothing to do here, then.
     if (!$record instanceof \Bolt\Legacy\Content) {
         return;
     }
     if (!is_array($exclude)) {
         $exclude = array_map('trim', explode(',', $exclude));
     }
     $context = ['record' => $record, 'common' => $common, 'extended' => $extended, 'repeaters' => $repeaters, 'exclude' => $exclude];
     return new \Twig_Markup($env->render($template, $context), 'utf-8');
 }
 /**
  * asset 方法,以解决网站处于子目录时的前台调用文件包含路径问题。
  *
  * @param  \Twig_Environment $twig
  * @param $asset
  * @return string
  */
 public function asset(\Twig_Environment $twig, $asset)
 {
     $globals = $twig->getGlobals();
     $request = $globals['app']['request'];
     return sprintf('%s/%s', $request->getBasePath(), ltrim($asset, '/'));
 }
 /**
  * @group legacy
  */
 public function testRemoveExtension()
 {
     $twig = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock());
     $twig->addExtension(new Twig_Tests_EnvironmentTest_Extension_WithDeprecatedName());
     $twig->removeExtension('environment_test');
     $this->assertFalse(array_key_exists('test', $twig->getTags()));
     $this->assertFalse(array_key_exists('foo_filter', $twig->getFilters()));
     $this->assertFalse(array_key_exists('foo_function', $twig->getFunctions()));
     $this->assertFalse(array_key_exists('foo_test', $twig->getTests()));
     $this->assertFalse(array_key_exists('foo_unary', $twig->getUnaryOperators()));
     $this->assertFalse(array_key_exists('foo_binary', $twig->getBinaryOperators()));
     $this->assertFalse(array_key_exists('foo_global', $twig->getGlobals()));
     $this->assertCount(2, $twig->getNodeVisitors());
 }
 /**
  * Return HTML of flash messages according to flash-messages.html.twig.
  *
  * @param \Twig_Environment $environment
  * @param array             $options
  *                                       - button_close : display a close button
  *                                       - trans_domain set the domain for translation
  *
  * @return string
  */
 public function flashMessages($environment, array $options = array())
 {
     $params = array_merge(array('close_button' => true, 'trans_domain' => null), $options);
     $template = $environment->loadTemplate('ASFLayoutBundle:session:flash-messages.html.twig');
     return $template->renderBlock('flash_messages', array_merge($environment->getGlobals(), $params));
 }
 public function testGlobals()
 {
     $globals = $this->twig->getGlobals();
     $this->assertEquals($this->mediaStorage, $globals['MediaStorage_instance']);
     $this->assertTrue($globals['MediaStorage_instance_debug']);
 }
Example #19
0
 public function issetVar(\Twig_Environment $environment, $varName)
 {
     $globals = $environment->getGlobals();
     return array_key_exists($varName, $globals);
 }
 /**
  * Render block
  */
 public function renderBlock($block, $parameters)
 {
     $template = $this->environment->loadTemplate(self::DEFAULT_TEMPLATE);
     return $template->renderBlock($block, array_merge($this->environment->getGlobals(), $parameters));
 }
Example #21
0
 private function getCarewGlobals(\Twig_Environment $twig)
 {
     $globals = $twig->getGlobals();
     return $globals['carew'];
 }
Example #22
0
 /**
  * Get existing global template variable
  * or return default value
  *
  * @param $varname
  * @param null $default
  * @return null
  */
 public function get($varname, $default = null)
 {
     $globals = $this->twig->getGlobals();
     return isset($globals[$varname]) ? $globals[$varname] : $default;
 }
Example #23
0
 /**
  * Get the global Twig context
  *
  * @return array
  */
 public function getGlobalContext()
 {
     return $this->twig->getGlobals();
 }
Example #24
0
 /**
  * Render block $block with $table view's data.
  * @param \Twig_Environment $twig
  * @param \EMC\TableBundle\Table\TableView $view
  * @return string
  */
 public function render(\Twig_Environment $twig, TableView $view, $template = null)
 {
     $context = array_merge($twig->getGlobals(), $view->getData());
     return $twig->loadTemplate($template ?: $this->template)->renderBlock('table', $context);
 }
Example #25
0
function twig_asset_url(Twig_Environment $env, $asset)
{
    $globals = $env->getGlobals();
    return BB_URL . 'bb-themes/' . $globals['current_theme'] . '/assets/' . $asset;
}
 /**
  * Not sure if this works ... doesn't appear to see all the globals.
  * If this works we could search the globals for a Google Analytics ID as well.
  *
  * @param Twig_Environment $environment
  * @param string           $name
  *
  * @return null
  */
 private function getGlobal(\Twig_Environment $environment, $name)
 {
     foreach ($environment->getGlobals() as $k => $v) {
         if ($k == $name) {
             return $v;
         }
     }
     return null;
 }
Example #27
0
 public function testAddExtension()
 {
     $twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
     $twig->addExtension(new Twig_Tests_EnvironmentTest_Extension());
     $this->assertArrayHasKey('test', $twig->getTags());
     $this->assertArrayHasKey('foo_filter', $twig->getFilters());
     $this->assertArrayHasKey('foo_function', $twig->getFunctions());
     $this->assertArrayHasKey('foo_test', $twig->getTests());
     $this->assertArrayHasKey('foo_unary', $twig->getUnaryOperators());
     $this->assertArrayHasKey('foo_binary', $twig->getBinaryOperators());
     $this->assertArrayHasKey('foo_global', $twig->getGlobals());
     $visitors = $twig->getNodeVisitors();
     $this->assertEquals('Twig_Tests_EnvironmentTest_NodeVisitor', get_class($visitors[2]));
 }
Example #28
0
 /**
  * Create an URL.
  *
  * @param \Twig_Environment $env
  * @param null              $value
  *
  * @return string
  */
 public function createUrl(\Twig_Environment $env, $value = null)
 {
     $baseurl = $env->getGlobals()['site']['baseurl'];
     if ($value instanceof Page) {
         $value = $value->getPermalink();
         $url = rtrim($baseurl, '/') . '/' . ltrim($value, '/');
     } else {
         if (preg_match('~^(?:f|ht)tps?://~i', $value)) {
             $url = $value;
         } elseif (false !== strpos($value, '.')) {
             $url = rtrim($baseurl, '/') . '/' . ltrim($value, '/');
         } else {
             $value = $this->slugifyFilter($value);
             $url = rtrim($baseurl, '/') . '/' . ltrim($value, '/');
         }
     }
     return $url;
 }
 /**
  * Return HTML of flash messages according to flash_alerts.html.twig
  *
  * @param array $options
  * 	- button_close : display a close button
  * 	- trans_domain set the domain for translation
  * @return string
  */
 public function flashMessages(array $options = array())
 {
     $params = array_merge(array('button_close' => true, 'trans_domain' => null), $options);
     $template = $this->environment->loadTemplate('O2UIBundle::flashalerts_template.html.twig');
     return $template->renderBlock('flash_messages', array_merge($this->environment->getGlobals(), $params));
 }
Example #30
0
 function assetLink(\Twig_Environment $twig, $path)
 {
     $globals = $twig->getGlobals();
     return $globals["app"]["pipe"]->assetLink($path);
 }