Commit 2dd0d7e5 by Seldaek

git-svn-id: svn://dwoo.org/dwoo/trunk@137 0598d79b-80c4-4d41-97ba-ac86fbbd088b

parent 03f8ba13
<?php
/**
* PluginProxy class for Zend View
*
* @package Dwoo
* @author Denis Arh <denis@arh.cc>
*/
class Dwoo_Adapters_ZendFramework_PluginProxy implements Dwoo_IPluginProxy
{
/**
* reference to the zend view owning this proxy
*
* @var Zend_View_Interface
*/
public $view;
/**
* Dwoo_Adapters_ZendFramework_PluginProxy's constructor.
*
* @param Zend_View_Interface $view
*/
public function __construct(Zend_View_Interface $view) {
$this->view = $view;
}
/**
* Called from Dwoo_Compiler to check if the requested plugin is available
*
* @param string $name
* @return bool
*/
public function loadPlugin($name) {
try {
$this->view->getHelper($name);
} catch (Zend_Loader_PluginLoader_Exception $e) {
return false;
}
return true;
}
/**
* Catch-all method for Zend view helpers. It generates code for
* Dwoo templates.
*
* @param string $name Name of the view helper
* @param array $args Helper's parameters
* @return string
*/
public function __call($name, $args) {
return '$this->getPluginProxy()->view->'. $name .'('.Dwoo_Compiler::implode_r($args).')';
}
}
\ No newline at end of file
// ------------------------
// Usage example :
// ------------------------
// Note that you might need to manually include 'lib/Dwoo.php',
// 'lib/Dwoo/Adapters/ZendFramework/View.php' and
// 'lib/Dwoo/Adapters/ZendFramework/PluginProxy.php' for this to
// work as expected, depending on your ZF setup
//
// If anyone writes a more advanced how-to please let me know
// ------------------------
$viewInterface = new Dwoo_Adapters_ZendFramework_View(array(
'compile_dir' => 'path/to/compile_dir' // set to null or remove this line to use defaults
'cache_dir' => 'path/to/cache_dir' // set to null or remove this line to use defaults
));
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($viewInterface);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
\ No newline at end of file
<?php
/**
* View interface for Dwoo template engine based on
* Zend_View_Abstract
*
* @package Dwoo
* @author Denis Arh <denis@arh.cc>
* @author Stephan Wentz <stephan@wentz.it>
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class Dwoo_Adapters_ZendFramework_View extends Zend_View_Abstract
{
/**
* @var Dwoo
*/
protected $_engine = null;
/**
*
* @var Dwoo_Data
*/
protected $_data = null;
/**
*
* @var Dwoo_IPluginProxy
*/
protected $_pluginProxy = null;
protected $_scriptPaths;
public function __construct(array $opt = array())
{
if (!isset($opt['compile_dir'])) {
$opt['compile_dir'] = null;
}
if (!isset($opt['cache_dir'])) {
$opt['cache_dir'] = null;
}
$this->_engine = new Dwoo($opt['compile_dir'], $opt['cache_dir']);
$this->_data = new Dwoo_Data;
}
/**
* Called before template rendering
*
* Binds plugin proxy to the Dwoo.
*
* @see Dwoo_Adapters_ZendFramework_View::getPluginProxy();
* @see Dwoo::setPluginProxy();
*/
protected function preRender()
{
$this->_engine->setPluginProxy($this->getPluginProxy());
}
/**
* Set the path to find the view script used by render()
*
* @param string|array The directory (-ies) to set as the path. Note that
* the concrete view implentation may not necessarily support multiple
* directories.
*
* @return void
*/
public function setScriptPath($path)
{
$this->_scriptPaths = (array) $path;
}
/**
* Retrieve all view script paths
*
* @return array
*/
public function getScriptPaths()
{
return $this->_scriptPaths;
}
/**
* Set a base path to all view resources
*
* @param string $path Base path
* @param string $classPrefix Class prefix
*
* @return void
*/
public function setBasePath($path, $classPrefix = 'Zend_View')
{
// Not supported by Dwoo
}
/**
* Add an additional path to view resources
*
* @param string $path Base path
* @param string $classPrefix Class prefix
* @return void
*/
public function addBasePath($path, $classPrefix = 'Zend_View')
{
// Not supported by Dwoo
}
/**
* Wraper for Dwoo_Data::__set()
* allows to assign variables using the object syntax
*
* @param string $name the variable name
* @param string $value the value to assign to it
*/
public function __set($name, $value)
{
$this->_data->__set($name, $value);
}
public function __get($name)
{
return $this->_data->__get($name);
}
/**
* Wraper for Dwoo_Data::__isset()
* supports calls to isset($dwooData->var)
*
* @param string $name the variable name
*/
public function __isset($name)
{
return $this->_data->__isset($name);
}
/**
* Wraper for Dwoo_Data::_unset()
* supports unsetting variables using the object syntax
*
* @see Dwoo_Data::_unset()
* @param string $name the variable name
*/
public function __unset($name)
{
$this->_data->__unset($name);
}
/**
* Returns plugin proxy interface
*
* @return Dwoo_IPluginProxy
*/
public function getPluginProxy()
{
if (!$this->_pluginProxy) {
$this->_pluginProxy = new Dwoo_Adapters_ZendFramework_PluginProxy($this);
}
return $this->_pluginProxy;
}
/**
* Adds plugin proxy
*
* @param Dwoo_IPluginProxy
* @return Dwoo_Adapters_ZendFramework_View
*/
public function setPluginProxy(Dwoo_IPluginProxy $pluginProxy)
{
$this->_pluginProxy = $pluginProxy;
return $this;
}
/**
* Returns data object
*
* @return Dwoo_Data
*/
public function getData()
{
return $this->_data;
}
/**
* Passes data to Dwoo_Data object
*
* @see Dwoo_Data::assign()
* @param array|string $name
* @param mixed $val
* @return Dwoo_Adapters_ZendFramework_View
*/
public function assign($name, $val = null)
{
$this->_data->assign($name, $val);
return $this;
}
/**
* Return the Dwoo template engine object
*
* @return Dwoo
*/
public function getEngine()
{
return $this->_engine;
}
/**
* Clear all assigned variables
*
* Clears all variables assigned to Zend_View either via {@link assign()} or
* property overloading ({@link __get()}/{@link __set()}).
*
* @return void
*/
public function clearVars()
{
$this->_data->clear();
}
public function render($name) {
$this->preRender();
return $this->_filter($this->_run($name));
}
/**
* Processes a view script and returns the output.
*
* @param string $name The script script name to process.
* @return string The script output.
*/
public function _run($name)
{
$tpl = new Dwoo_Template_File($name, null, null, null, $this->_scriptPaths);
return $this->_engine->get($tpl, $this->_data);
}
/**
* Add plugin path
*
* @param string $dir Directory
*/
public function addPluginDir($dir)
{
$this->_engine->getLoader()->addDirectory($dir);
}
/**
* Set compile path
*
* @param string $dir Directory
*/
public function setCompileDir($dir)
{
$this->_engine->setCompileDir($dir);
}
/**
* Set cache path
*
* @param string $dir Directory
*/
public function setCacheDir($dir)
{
$this->_engine->setCacheDir($dir);
}
/**
* Set cache lifetime
*
* @param string $seconds Lifetime in seconds
*/
public function setCacheLifetime($seconds)
{
$this->_engine->setCacheTime($seconds);
}
/**
* Set charset
*
* @param string $charset
*/
public function setCharset($charset)
{
$this->_engine->setCharset($charset);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment