Skip to content

Instantly share code, notes, and snippets.

@weaverryan
Last active July 9, 2018 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weaverryan/731fe86232b61228e5ac to your computer and use it in GitHub Desktop.
Save weaverryan/731fe86232b61228e5ac to your computer and use it in GitHub Desktop.
Twig Extension with ContainerInterface injected
<?php
class UniversityExtension extends \Twig_Extension
{
private $container;
/**
* An internal cache used to prevent creating this chain loader multiple times
*/
private $twigChainLoader;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* {@inheritdoc}
*/
public function getTests()
{
return array(
'paid' => new \Twig_Test_Method($this, 'isPaid'),
'viewed' => new \Twig_Test_Method($this, 'isViewed'),
'solved' => new \Twig_Test_Method($this, 'isSolved'),
);
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return array(
'absolute_asset' => new \Twig_Function_Method($this, 'getAbsoluteAssetUrl'),
'get_template' => new \Twig_Function_Method($this, 'getTemplate', array('is_safe' => array('html'))),
new \Twig_SimpleFunction('get_client_template', array($this, 'getClientTemplate'), array('is_safe' => array('html'))),
new \Twig_SimpleFunction('get_comment_count', array($this, 'getCommentCount')),
new \Twig_SimpleFunction('get_main_contributor', array($this, 'getMainContributor')),
new \Twig_SimpleFunction('get_other_contributors', array($this, 'getOtherContributors')),
new \Twig_SimpleFunction('get_all_contributors', array($this, 'getAllContributors')),
new \Twig_SimpleFunction('get_chapter_number', array($this, 'getChapterNumber')),
new \Twig_SimpleFunction('get_next_url', array($this, 'getNextUrl')),
new \Twig_SimpleFunction('get_new_order', array($this, 'getNewOrder')),
new \Twig_SimpleFunction('get_stripe_public_key', array($this, 'getStripePublicKey')),
new \Twig_SimpleFunction('get_last_subscription_order', array($this, 'getLastSubscriptionOrder')),
new \Twig_SimpleFunction('get_current_order', array($this, 'getCurrentOrder')),
);
}
public function getFilters()
{
return array(
new \Twig_SimpleFilter('normalize', array($this, 'normalize')),
new \Twig_SimpleFilter('format_price', array($this, 'formatPrice')),
new \Twig_SimpleFilter('build_status_label', array($this, 'buildStatusLabel')),
new \Twig_SimpleFilter('illustration_path', array($this, 'getBuildIllustrationPath')),
new \Twig_SimpleFilter('build_highlight', array($this, 'highlightBuildNotes')),
new \Twig_SimpleFilter('subscription_price', array($this, 'getSubscriptionPrice')),
new \Twig_SimpleFilter('github_markdown', array($this, 'parseGitubMarkdown')),
new \Twig_SimpleFilter('secure', array($this, 'makeUrlSecure')),
new \Twig_SimpleFilter('asset_version', array($this, 'getAssetVersion')),
);
}
public function normalize($object)
{
$normalizer = $this->container->get('normalizer');
return $normalizer->normalize($object);
}
public function getMainContributor(Blog $blog)
{
$mainContributor = $this->getEntityManager()->getRepository('KnpUniversityBundle:BlogContributor')
->findMainContributor($blog)
;
if (!$mainContributor) {
throw new \Exception(sprintf('Could not find any contributors for blog "%s"', $blog->getId()));
}
return $mainContributor;
}
public function getAllContributors(Blog $blog)
{
$contributors = $this->getEntityManager()->getRepository('KnpUniversityBundle:BlogContributor')
->findAllContributors($blog)
;
return $contributors;
}
public function getChapterNumber(CourseChapter $chapter)
{
return $this->getEntityManager()->getRepository('KnpUniversityBundle:CourseChapter')
->getChapterNumber($chapter)
;
}
public function getBuildIllustrationPath(CourseBuild $build)
{
return $this->container->get('knp_university.storage.public_storage')
->getIllustrationPublicPath($build)
;
}
public function getStripePublicKey()
{
return $this->container->get('knp_university.stripe.manager')
->getPublicKey();
}
public function parseGitubMarkdown($str)
{
return $this->container->get('markdown_parser')
->parse($str);
}
}
@duqingnian
Copy link

Fatal error: Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException' with message 'Catchable Fatal Error: Argument 1 passed to FrontBundle\Twig\FrontExtension::__construct() must be an instance of FrontBundle\Twig\ContainerInterface, none given

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment