Kohana_Twig_Environment::instance PHP Method

instance() public static method

Loads Twig_Environments based on the configuration key they represent
Author: Jonathan Geiger
public static instance ( string $env = 'default' ) : Twig_Environment
$env string
return Twig_Environment
    public static function instance($env = 'default')
    {
        static $instances;
        if (!isset($instances[$env])) {
            $config = Kohana::$config->load('twig.' . $env);
            // Create the the loader
            $twig_loader = $config['loader']['class'];
            $loader = new $twig_loader($config['loader']['options']);
            // Set up the instance
            $twig = $instances[$env] = new Twig_Environment($loader, $config['environment']);
            // Load extensions
            foreach ($config['extensions'] as $extension) {
                $twig->addExtension(new $extension());
            }
            // Add the sandboxing extension.
            // The sandbox seems buggy
            // So this dummy condition is there to avoid the bug
            // The error thrown is "Twig_Sandbox_SecurityError [ 0 ]: Calling "__toString" method on a "Twig" object is not allowed."
            if (!empty($config['sandboxing']['tags']) && !empty($config['sandboxing']['filters']) && !empty($config['sandboxing']['methods']) && !empty($config['sandboxing']['properties'])) {
                $policy = new Twig_Sandbox_SecurityPolicy($config['sandboxing']['tags'], $config['sandboxing']['filters'], $config['sandboxing']['methods'], $config['sandboxing']['properties']);
                $twig->addExtension(new Twig_Extension_Sandbox($policy, $config['sandboxing']['global']));
            }
        }
        return $instances[$env];
    }

Usage Example

コード例 #1
0
ファイル: Twig.php プロジェクト: nex2hex/kohana-twig
 /**
  * Constructor
  *
  * @param array $data 
  * @author Jonathan Geiger
  */
 public function __construct($file = NULL, $data = NULL, $env = 'default')
 {
     parent::__construct($file, $data);
     // Allow passing a Twig_Environment
     if (is_string($env)) {
         $env = Kohana_Twig_Environment::instance($env);
     }
     $this->_environment = $env;
 }
All Usage Examples Of Kohana_Twig_Environment::instance
Kohana_Twig_Environment