Created
April 9, 2012 19:03
-
-
Save harikt/2345593 to your computer and use it in GitHub Desktop.
Trying to make use of Twig than Aura.View, just vague idea, still need to think more on how to make a better way. How to change from controller etc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* This file is part of the Aura project for PHP. | |
* | |
* @license http://opensource.org/licenses/bsd-license.php BSD | |
* | |
*/ | |
namespace Hari\Twig\Web; | |
use Aura\Framework\Inflect; | |
use Aura\Framework\System; | |
use Aura\Router\Map as RouterMap; | |
use Aura\Signal\Manager as SignalManager; | |
use Aura\Web\AbstractPage as WebAbstractPage; | |
/** | |
* | |
* An abstract web page controller for the framework. | |
* | |
* @package Hari.Twig | |
* | |
*/ | |
abstract class AbstractPage extends WebAbstractPage | |
{ | |
/** | |
* | |
* An inflection object. | |
* | |
* @var Inflect | |
* | |
*/ | |
protected $inflect; | |
/** | |
* | |
* A router object. | |
* | |
* @var RouterMap | |
* | |
*/ | |
protected $router; | |
/** | |
* | |
* A signal manager | |
* | |
* @var SignalManager | |
* | |
*/ | |
protected $signal; | |
/** | |
* | |
* A system object. | |
* | |
* @var System | |
* | |
*/ | |
protected $system; | |
protected $twig; | |
protected $loader; | |
protected $inner_view; | |
/** | |
* | |
* Sets the inflection object. | |
* | |
* @param Inflect $inflect The inflection object. | |
* | |
* @return void | |
* | |
*/ | |
public function setInflect(Inflect $inflect) | |
{ | |
$this->inflect = $inflect; | |
} | |
/** | |
* | |
* Sets the router object. | |
* | |
* @param RouterMap $router The router object. | |
* | |
* @return void | |
* | |
*/ | |
public function setRouter(RouterMap $router) | |
{ | |
$this->router = $router; | |
} | |
/** | |
* | |
* Sets the signal manager and adds handlers for hooks. | |
* | |
* @param SignalManager $signal The signal manager. | |
* | |
* @return void | |
* | |
*/ | |
public function setSignal(SignalManager $signal) | |
{ | |
$this->signal = $signal; | |
$this->signal->handler($this, 'pre_exec', [$this, 'preExec']); | |
$this->signal->handler($this, 'pre_action', [$this, 'preAction']); | |
$this->signal->handler($this, 'post_action', [$this, 'postAction']); | |
$this->signal->handler($this, 'pre_render', [$this, 'preRender']); | |
$this->signal->handler($this, 'post_render', [$this, 'postRender']); | |
$this->signal->handler($this, 'post_exec', [$this, 'postExec']); | |
} | |
/** | |
* | |
* Sets the system object. | |
* | |
* @param System $system The system object. | |
* | |
* @return void | |
* | |
*/ | |
public function setSystem(System $system) | |
{ | |
$this->system = $system; | |
} | |
/** | |
* | |
* Executes the page action, invoking hooks via the signal manager. | |
* | |
* @return Aura\Web\Response | |
* | |
*/ | |
public function exec() | |
{ | |
// prep | |
$this->signal->send($this, 'pre_exec', $this); | |
// the action cycle | |
$this->signal->send($this, 'pre_action', $this); | |
$this->action(); | |
$this->signal->send($this, 'post_action', $this); | |
// the render cycle | |
$this->signal->send($this, 'pre_render', $this); | |
$this->render(); | |
$this->signal->send($this, 'post_render', $this); | |
// done | |
$this->signal->send($this, 'post_exec', $this); | |
return $this->response; | |
} | |
public function setTwigLoaderInterface(\Twig_LoaderInterface $loader) | |
{ | |
$this->loader = $loader; | |
/** | |
* Here we only assume the template is in the particular folder | |
* But what about moving to another package? | |
* Extends works, but we may want to add something to load it too | |
*/ | |
$includes = array_reverse(get_included_files()); | |
// get the class hierarchy stack | |
$class = get_class($this); | |
$stack = class_parents($class); | |
// drop Aura.Web and Aura.Framework | |
array_pop($stack); | |
array_pop($stack); | |
// add this class itself | |
array_unshift($stack, $class); | |
// go through the hierarchy and look for each class file | |
// Nb: this will not work if we concatenate all the classes into a | |
// single file. | |
foreach ($stack as $class) { | |
$match = $this->inflect->classToFile($class); | |
$len = strlen($match) * -1; | |
foreach ($includes as $i => $include) { | |
if (substr($include, $len) == $match) { | |
$dir = dirname($include); | |
$this->loader->setPaths([ | |
$dir . DIRECTORY_SEPARATOR . 'view', | |
$dir . DIRECTORY_SEPARATOR . 'layout' | |
]); | |
unset($includes[$i]); | |
break; | |
} | |
} | |
} | |
} | |
public function setTwigEnvironment(\Twig_Environment $twig) | |
{ | |
$this->twig = $twig; | |
} | |
protected function setInnerView($name) | |
{ | |
$this->inner_view = $name; | |
} | |
protected function getInnerView() | |
{ | |
return $this->inner_view; | |
} | |
/** | |
* | |
* Renders the view into the response and sets the response content-type. | |
* | |
* N.b.: If the response content is already set, the view will not be | |
* rendered. | |
* | |
* @return void | |
* | |
*/ | |
protected function render() | |
{ | |
if (! $this->response->getContent()) { | |
$inner_view = $this->getInnerView(); | |
//throw execption if template not set ? | |
$inner_view = isset($inner_view) ? $inner_view : 'hello'; | |
$format = $this->getFormat(); | |
$format = isset($format) ? $format : '.html'; | |
$this->response->setContent( | |
$this->twig->render( | |
$inner_view . $format, | |
(array) $this->getData() | |
) | |
); | |
} | |
// Create a FormatType object and getContentType from it | |
// getFormat() | |
// $this->response->setContentType($this->getContentType($this->getFormat())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$loader->add('Hari\Twig\\', dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src'); | |
$root_dir = dirname(dirname(dirname(__DIR__))); | |
$loader->add('Twig_', $root_dir . | |
DIRECTORY_SEPARATOR . | |
'include' . | |
DIRECTORY_SEPARATOR . | |
'Twig' . | |
DIRECTORY_SEPARATOR . | |
'lib'); | |
$di->set('twig_loader', function() use ($di, $root_dir) { | |
return $di->newInstance('\Twig_Loader_Filesystem', ['paths' => | |
$root_dir . '/web/' | |
]); | |
}); | |
$di->params['\Twig_Environment'] = [ | |
'loader' => $di->lazyGet('twig_loader'), | |
'options' => [ | |
'cache' => $root_dir . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'cache', | |
'debug' => true | |
] | |
]; | |
$di->set('twig_environment', function() use ($di) { | |
return $di->newInstance('\Twig_Environment'); | |
}); | |
$di->setter['Hari\Twig\Web\AbstractPage'] = [ | |
'setInflect' => $di->lazyGet('framework_inflect'), | |
'setRouter' => $di->lazyGet('router_map'), | |
'setSignal' => $di->lazyGet('signal_manager'), | |
'setSystem' => $di->lazyGet('framework_system'), | |
'setTwigLoaderInterface' => $di->lazyGet('twig_loader'), | |
'setTwigEnvironment' => $di->get('twig_environment'), | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello {{ name }}! | |
We want to work on inheritance, ie from different packages. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// A simple controller | |
namespace Hari\Blog\Web\Post; | |
//use Aura\Framework\Web\AbstractPage; | |
use Hari\Twig\Web\AbstractPage; | |
class Page extends AbstractPage | |
{ | |
public function preExec() | |
{ | |
//$this->view->setOuterView('layout'); | |
} | |
public function actionAdd() | |
{ | |
//$this->view->setInnerView('add'); | |
} | |
public function actionEdit() | |
{ | |
//$this->view->setInnerView('edit'); | |
} | |
public function actionDelete() | |
{ | |
//$this->view->setInnerView('delete'); | |
} | |
public function actionView() | |
{ | |
//$this->view->setInnerView('view'); | |
} | |
public function actionList() | |
{ | |
$this->setInnerView('list'); | |
$this->data->name = 'Hari K T'; | |
/* | |
$this->view->setInnerData([[ | |
'datas' => '<object data="data:text/html;base64,PHNjcmlwdD5hbGVydCgiSGV | |
sbG8iKTs8L3NjcmlwdD4="></object>', | |
]]); | |
$this->view->setInnerView('list'); | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment