Skip to content
Browse files

provide a twig extension to use cache manager in twig template

  • Loading branch information...
1 parent 7d68bac commit 371ccd3964977ac7481d78d49bd78f82e99c2e6e @kelu95 committed Jun 16, 2011
Showing with 134 additions and 0 deletions.
  1. +26 −0 DependencyInjection/ApcExtension.php
  2. +96 −0 Extension/ApcCacheExtension.php
  3. +12 −0 Resources/config/apc_cache_services.xml
View
26 DependencyInjection/ApcExtension.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace Kelu95\ApcBundle\DependencyInjection;
+
+use Symfony\Component\Config\FileLocator;
+use Symfony\Component\HttpKernel\DependencyInjection\Extension;
+use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+
+class ApcExtension extends Extension
+{
+ public function load(array $config, ContainerBuilder $container)
+ {
+ if (!$container->hasDefinition('apc_cache'))
+ {
+ $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
+ $loader->load('apc_cache_services.xml');
+
+ }
+ }
+
+ public function getAlias()
+ {
+ return 'apc';
+ }
+}
View
96 Extension/ApcCacheExtension.php
@@ -0,0 +1,96 @@
+<?php
+namespace Kelu95\ApcBundle\Extension;
+
+use Symfony\Component\HttpKernel\KernelInterface;
+use Symfony\Bundle\TwigBundle\Loader\FilesystemLoader;
+use Symfony\Component\Routing\RouterInterface;
+use Symfony\Component\HttpFoundation\Request;
+
+/*
+ * Call example
+ *
+ * {% if not cache('my_cache_var') %}
+ * my html code
+ * {% endif %}
+ * {{ cache_save()|raw }}
+ * */
+class ApcCacheExtension extends \Twig_Extension
+{
+ protected $loader;
+ protected $controller;
+ protected $environment;
+ protected $container;
+ protected $cache;
+ protected $cache_exist=false;
+
+ public function __construct($container)
+ {
+ $this->container = $container;
+ $this->cache = $this->container->get('apc_cache'); //load apc cache service
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function initRuntime(\Twig_Environment $environment)
+ {
+ $this->environment = $environment;
+ }
+
+ public function setController($controller)
+ {
+ $this->controller = $controller;
+ }
+
+ public function getName()
+ {
+ return 'apc_cache';
+ }
+
+
+ /**
+ * Returns a list of functions to add to the existing list.
+ *
+ * @return array An array of functions
+ */
+ public function getFunctions()
+ {
+ return array(
+ 'cache' => new \Twig_Function_Method($this, 'cache'),
+ 'cache_save' => new \Twig_Function_Method($this, 'cacheSave'),
+ );
+ }
+
+ /*
+ * Test is a cache variable exist and start output buffering if needed
+ *
+ * @param string $name
+ * @return true or false
+ *
+ * */
+ public function cache($name)
+ {
+ if(!$this->cache->exist($name)){
+ ob_start();
+ return false;
+ }
+ $this->cache_exist=true;
+ return true;
+ }
+
+ /*
+ * Save the cahce from output buffering or return cached datas
+ *
+ * @return mixed value
+ *
+ * */
+ public function cacheSave()
+ {
+ if(!$this->cache_exist){
+ $out = ob_get_contents();
+ ob_end_clean();
+ $this->cache->add($out);
+ }
+ return $this->cache->get();
+ }
+}
View
12 Resources/config/apc_cache_services.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<container xmlns="http://symfony.com/schema/dic/services"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
+
+ <services>
+ <service id="apc_cache_twig" class="Kelu95\ApcBundle\Extension\ApcCacheExtension">
+ <argument type="service" id="service_container" />
+ <tag name="twig.extension" />
+ </service>
+ </services>
+</container>

0 comments on commit 371ccd3

Please sign in to comment.
Something went wrong with that request. Please try again.