本文整理汇总了PHP中Twig_Environment::getExtension方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Environment::getExtension方法的具体用法?PHP Twig_Environment::getExtension怎么用?PHP Twig_Environment::getExtension使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_Environment
的用法示例。
在下文中一共展示了Twig_Environment::getExtension方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validate
/**
* @param EmailTemplate $value
* @param Constraint|EmailTemplateSyntax $constraint
*/
public function validate($value, Constraint $constraint)
{
// prepare templates to be validated
$itemsToValidate = [['field' => 'subject', 'locale' => null, 'template' => $value->getSubject()], ['field' => 'content', 'locale' => null, 'template' => $value->getContent()]];
$translations = $value->getTranslations();
foreach ($translations as $trans) {
if (in_array($trans->getField(), ['subject', 'content'])) {
$itemsToValidate[] = ['field' => $trans->getField(), 'locale' => $trans->getLocale(), 'template' => $trans->getContent()];
}
}
/** @var \Twig_Extension_Sandbox $sandbox */
$sandbox = $this->twig->getExtension('sandbox');
$sandbox->enableSandbox();
// validate templates' syntax
$errors = [];
foreach ($itemsToValidate as &$item) {
try {
$this->twig->parse($this->twig->tokenize($item['template']));
} catch (\Twig_Error_Syntax $e) {
$errors[] = ['field' => $item['field'], 'locale' => $item['locale'], 'error' => $e->getMessage()];
}
}
$sandbox->disableSandbox();
// add violations for found errors
if (!empty($errors)) {
foreach ($errors as $error) {
$this->context->addViolation($constraint->message, ['{{ field }}' => $this->getFieldLabel(ClassUtils::getClass($value), $error['field']), '{{ locale }}' => $this->getLocaleName($error['locale']), '{{ error }}' => $error['error']]);
}
}
}
示例2: configure
public function configure(\Twig_Environment $environment)
{
$environment->getExtension('core')->setDateFormat($this->dateFormat, $this->intervalFormat);
if (null !== $this->timezone) {
$environment->getExtension('core')->setTimezone($this->timezone);
}
$environment->getExtension('core')->setNumberFormat($this->decimals, $this->decimalPoint, $this->thousandsSeparator);
}
示例3: extractTemplate
protected function extractTemplate($template, MessageCatalogue $catalogue)
{
$visitor = $this->twig->getExtension('Symfony\\Bridge\\Twig\\Extension\\TranslationExtension')->getTranslationNodeVisitor();
$visitor->enable();
$this->twig->parse($this->twig->tokenize(new \Twig_Source($template, '')));
foreach ($visitor->getMessages() as $message) {
$catalogue->set(trim($message[0]), $this->prefix . trim($message[0]), $message[1] ?: $this->defaultDomain);
}
$visitor->disable();
}
示例4: extractTemplate
protected function extractTemplate($template, MessageCatalogue $catalogue)
{
$visitor = $this->twig->getExtension('translator')->getTranslationNodeVisitor();
$visitor->enable();
$this->twig->parse($this->twig->tokenize($template));
foreach ($visitor->getMessages() as $message) {
$catalogue->set(trim($message[0]), $this->prefix . trim($message[0]), $message[1] ? $message[1] : $this->defaultDomain);
}
$visitor->disable();
}
示例5: onKernelRequest
/**
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
if ($event->getRequestType() != HttpKernelInterface::MASTER_REQUEST) {
return;
}
$securityContext = $this->securityContext;
if ($securityContext && $securityContext->getToken() instanceof TokenInterface && $securityContext->getToken()->getUser() instanceof TimeZoneInterface) {
$timeZone = $securityContext->getToken()->getUser()->getTimezone();
if ($timeZone) {
$this->twigEnvironment->getExtension('core')->setTimezone($timeZone);
}
}
}
示例6: includeAceEditor
/**
* Echoes the <script> tag.
*
*/
public function includeAceEditor()
{
if (!$this->environment->hasExtension('asset') || $this->editorIncluded) {
return;
}
if (!$this->editorIncluded) {
foreach (array('ace', 'ext-language_tools') as $file) {
$jsPath = $this->environment->getExtension('asset')->getAssetUrl($this->basePath . '/' . $this->mode . '/' . $file . '.js');
echo sprintf('<script src="%s" charset="utf-8" type="text/javascript"></script>', $jsPath);
}
$this->editorIncluded = true;
}
}
示例7: includeAceEditor
public function includeAceEditor()
{
if (!$this->environment->hasExtension('assets')) {
return;
}
if (!$this->editorIncluded) {
$this->editorIncluded = true;
}
if (!$this->ckeditorIncluded) {
$jsPath = $this->environment->getExtension('assets')->getAssetUrl($this->basePath . '/' . $this->mode);
echo sprintf('<script src="%s" charset="utf-8"></script>', $jsPath);
$this->ckeditorIncluded = true;
}
}
示例8: getTwig
/**
* Get Twig-instance
*
* @return Twig_Environment
*/
protected static function getTwig()
{
if (!static::$twig) {
$loader = new \Twig_Loader_Filesystem(PATH_APP . '/views');
static::$twig = new \Twig_Environment($loader, array('cache' => MAIN_SERVER ? PATH_DATA . '/twig-cache' : null, 'strict_variables' => true, 'autoescape' => false));
static::$twig->getExtension('core')->setNumberFormat(2, ',', ' ');
static::$twig->addExtension(new \Kofradia\Twig\Date());
static::$twig->addExtension(new \Kofradia\Twig\Counter());
static::$twig->addExtension(new \Kofradia\Twig\Render());
static::$twig->addExtension(new \Kofradia\Twig\Helpers());
static::$twig->addGlobal("page", \ess::$b->page);
static::$twig->addGlobal("helper", new \Kofradia\Twig\TemplateHelper());
}
return static::$twig;
}
示例9: doEnterNode
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
{
if ($node instanceof \Twig_Node_Block) {
$this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needCollapsing();
} elseif ($node instanceof WhitespaceCollapse) {
$this->statusStack[] = $node->getAttribute('value');
} elseif ($env->hasExtension('whitespace_collapser')) {
/** @var \MatTheCat\Twig\Extension\WhitespaceCollapser $extension */
$extension = $env->getExtension('whitespace_collapser');
$extensionDefault = $extension->getDefault();
if ($node instanceof \Twig_Node_Module) {
if (is_array($extensionDefault)) {
$filename = $node->getAttribute('filename');
if (substr($filename, -5) === '.twig') {
$filename = substr($filename, 0, -5);
}
$this->enabledByDefault = in_array(pathinfo($filename, PATHINFO_EXTENSION), $extensionDefault, true);
} else {
$this->enabledByDefault = $extensionDefault;
}
} elseif ($node instanceof \Twig_Node_AutoEscape) {
$this->statusStack[] = is_array($extensionDefault) ? in_array($node->getAttribute('value'), $extensionDefault) : $extensionDefault;
}
}
return $node;
}
示例10: checkNode
/**
* Extracts formulae from filter function nodes.
*
* @return array|null The formula
*/
private function checkNode(\Twig_NodeInterface $node, \Twig_Environment $env)
{
if ($node instanceof \Twig_Node_Expression_Function) {
$name = $node->getNode('name')->getAttribute('name');
if ($env->getFunction($name) instanceof AsseticFilterFunction) {
$arguments = array();
foreach ($node->getNode('arguments') as $argument) {
$arguments[] = eval('return '.$env->compile($argument).';');
}
$invoker = $env->getExtension('assetic')->getFilterInvoker($name);
$factory = $invoker->getFactory();
$inputs = isset($arguments[0]) ? (array) $arguments[0] : array();
$filters = $invoker->getFilters();
$options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array());
if (!isset($options['name'])) {
$options['name'] = $factory->generateAssetName($inputs, $filters);
}
return array($inputs, $filters, $options);
}
}
}
示例11: __construct
public function __construct(\Twig_Environment $twig, LoggerInterface $logger = null)
{
$this->twig = $twig;
$this->logger = $logger;
$globals = $twig->getExtension('assetic')->getGlobals();
$this->debug = $globals['assetic']['debug'];
}
示例12: renderFragment
/**
* Renders fragment by window state.
*
* @param \Twig_Environment $environment
* @param AbstractWindowsState $windowState
*
* @return string
*/
public function renderFragment(\Twig_Environment $environment, AbstractWindowsState $windowState)
{
$result = '';
$scheduleDelete = false;
$windowState->setRenderedSuccessfully(false);
try {
$uri = $this->windowsStateRequestManager->getUri($windowState->getData());
/** @var HttpKernelExtension $httpKernelExtension */
$httpKernelExtension = $environment->getExtension('http_kernel');
$result = $httpKernelExtension->renderFragment($uri);
$windowState->setRenderedSuccessfully(true);
return $result;
} catch (NotFoundHttpException $e) {
$scheduleDelete = true;
} catch (\InvalidArgumentException $e) {
$scheduleDelete = true;
}
if ($scheduleDelete) {
try {
$this->windowsStateManagerRegistry->getManager()->deleteWindowsState($windowState->getId());
} catch (AccessDeniedException $e) {
return $result;
}
}
return $result;
}
示例13: let
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);
}
示例14: testCustomEscaper
public function testCustomEscaper()
{
$twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
$twig->getExtension('core')->setEscaper('foo', 'foo_escaper_for_test');
$this->assertEquals('fooUTF-8', twig_escape_filter($twig, 'foo', 'foo'));
$this->assertEquals('UTF-8', twig_escape_filter($twig, null, 'foo'));
$this->assertEquals('42UTF-8', twig_escape_filter($twig, 42, 'foo'));
}
示例15: extractTemplate
/**
* Extract messages from a single template
*
* @param $templatePath
* @param \Symfony\Component\Translation\MessageCatalogue $catalogue
* @return string
*/
public function extractTemplate($templatePath, MessageCatalogue $catalogue)
{
$content = file_get_contents($templatePath);
$translator = $this->twig->getExtension('translator');
/**
* @var TranslationNodeVisitor $visitor
*/
$visitor = $translator->getTranslationNodeVisitor();
$visitor->enable();
$tokens = $this->twig->tokenize($content);
$this->twig->parse($tokens);
foreach ($visitor->getMessages() as $message) {
$catalogue->set(trim($message[0]), $this->prefix . trim($message[0]), $message[1] ? $message[1] : $this->defaultDomain);
}
$visitor->disable();
return $content;
}