Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Tuned up Vegas\Mvc\View #5

Merged
merged 1 commit into from
Sep 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Application/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public function setDi(DiInterface $di)
$this->di = $di;
}

/**
* Returns Dependency Injector
*
* @return FactoryDefault|DiInterface
*/
public function getDI()
{
return $this->di;
}

/**
* Initializes application environment
*/
Expand Down
13 changes: 11 additions & 2 deletions src/DI/Service/Component/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* file that was distributed with this source code.
*/
namespace Vegas\DI\Service\Component;
use Vegas\Mvc\View;

/**
* Class Renderer
Expand Down Expand Up @@ -71,7 +72,14 @@ public function setTemplateName($name)
*/
public function render($params = array())
{
return $this->view->partial($this->getServiceViewPath(), $params);
$currentViewsDir = $this->view->getViewsDir();
$this->view->setViewsDir($this->getServiceViewPath());
$this->view->disableLevel(View::LEVEL_LAYOUT);
$content = $this->view->getRender('services', $this->templateName, $params);

//rollback viewsDir
$this->view->setViewsDir($currentViewsDir);
return $content;
}

/**
Expand All @@ -80,6 +88,7 @@ public function render($params = array())
*/
private function getServiceViewPath()
{
return '../../'.$this->moduleName.'/views/services/'.$this->templateName;
$viewsDir = APP_ROOT . '/app/modules/' . $this->moduleName . '/views/';
return $viewsDir;
}
}
95 changes: 49 additions & 46 deletions src/Mvc/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,72 @@ class View extends PhalconView
* Constructor
* Prepares view settings and engine
*
* @override
* @param null $options
* @param null $viewDir
*/
public function __construct($options = null, $viewDir = null) {
parent::__construct($options);

if (isset($options['layoutsDir']) && $viewDir) {
$this->setLayoutsDir($this->prepareRelativeLayoutsPath($options, $viewDir));
if (isset($options['layoutsDir'])) {
$this->setLayoutsDir($options['layoutsDir']);
}

if (!empty($options['layout'])) {
if (isset($options['partialsDir'])) {
$this->setPartialsDir($options['partialsDir']);
} else {
// $this->setPartialsDir($options['layoutsDir'] . 'partials/');
}

if (isset($options['layout']) && !empty($options['layout'])) {
$this->setLayout($options['layout']);
}

$this->registerEngines(array(
'.volt' => function ($this, $di) use ($options) {
$volt = new \Vegas\Mvc\View\Engine\Volt($this, $di);
if (isset($options['cacheDir'])) {
$volt->setOptions(array(
'compiledPath' => $options['cacheDir'],
'compiledSeparator' => '_'
));
}
$volt->registerFilters();
$volt->registerHelpers();

return $volt;
},
$volt = new \Vegas\Mvc\View\Engine\Volt($this, $di);
if (isset($options['cacheDir'])) {
$volt->setOptions(array(
'compiledPath' => $options['cacheDir'],
'compiledSeparator' => '_'
));
}
$volt->registerFilters();
$volt->registerHelpers();
$volt->setExtension('.volt');

return $volt;
},
'.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));
}

/**
* Checks whether view exists on registered extensions and render it
*
* @override
* @param array $engines
* @param string $viewPath
* @param boolean $silence
* @param boolean $mustClean
* @param \Phalcon\Cache\BackendInterface $cache
*/
protected function _engineRender($engines, $viewPath, $silence, $mustClean, $cache)
{
//checks if layout template is rendered
//get rid of trailing slash
if (dirname($viewPath) == rtrim($this->getLayoutsDir(), DIRECTORY_SEPARATOR)) {
//when layouts is rendered change viewsDir to layoutsDir path
$this->setViewsDir($this->getLayoutsDir());
$viewPath = basename($viewPath);
}
parent::_engineRender($engines, $viewPath, $silence, $mustClean, $cache);
}

/**
* Renders view for controller action
*
* @oerride
* @param string $controllerName
* @param string $actionName
* @param null $params
Expand All @@ -74,7 +105,6 @@ public function render($controllerName, $actionName, $params = null) {
if (empty($this->controllerViewPath)) {
$this->controllerViewPath = $this->prepareControllerViewPath($controllerName);
}

parent::render($this->controllerViewPath, $actionName, $params);
}

Expand All @@ -87,37 +117,10 @@ public function render($controllerName, $actionName, $params = null) {
*/
private function prepareControllerViewPath($controllerName)
{
return str_replace('\\','/',strtolower($controllerName));
}

/**
* Prepares relative layout path
*
* @param array $options
* @param null $viewDir
* @return string
* @internal
*/
private function prepareRelativeLayoutsPath(array $options, $viewDir = null)
{
$path = str_replace(APP_ROOT, '', realpath($options['layoutsDir']));

$nbOfDirs = count(explode('/', $path));

$baseDepth = '';
for ($i=0; $i<$nbOfDirs; $i++) {
$baseDepth .= ($i ? '/' : '').'..';
}

if ($viewDir) {
$modPath = str_replace(APP_ROOT, '', realpath(dirname($viewDir)));
$nbOfDirs = count(explode('/', $modPath)) - $nbOfDirs;

for ($i=0; $i<$nbOfDirs; $i++) {
$path = '/..'.$path;
}
if (strpos($controllerName, '\\')) {
$controllerName = str_replace('\\','/',strtolower($controllerName));
}

return $baseDepth.$path;
return $controllerName;
}
}
77 changes: 75 additions & 2 deletions src/Mvc/View/Engine/Volt.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Vegas\Mvc\View\Engine;

use Vegas\Mvc\View\Engine\Volt\Exception\InvalidFilterException;
use Vegas\Mvc\View\Engine\Volt\Exception\UnknownFilterException;
use Vegas\Mvc\View\Engine\Volt\VoltFilterAbstract;
use Vegas\Mvc\View\Engine\Volt\VoltHelperAbstract;


/**
* Class Volt
* @package Vegas\Mvc\View\Engine
Expand All @@ -26,6 +27,23 @@ class Volt extends \Phalcon\Mvc\View\Engine\Volt
use RegisterFilters;
use RegisterHelpers;

/**
* Extension of template file
*
* @var string
*/
private $extension = '.volt';

/**
* Sets template file extension
*
* @param $extension
*/
public function setExtension($extension)
{
$this->extension = $extension;
}

/**
* Registers a new filter in the compiler
*
Expand Down Expand Up @@ -78,4 +96,59 @@ private function getClassInstance($className)
$reflectionClass = new \ReflectionClass($className);
return $reflectionClass->newInstance($this->getCompiler());
}
}

/**
* Renders a partial inside another view
*
* Uses partialsDir from config
* Methods check if partial is in local directory, if so then prepares full path to local file,
* otherwise uses global partialsDir path
*
* Before use setup partialsDir in application config (app/config/config.php):
* <code>
* //remember about trailing slashes
* 'application' => array(
* ...
* 'view' => array(
* 'layout' => 'main',
* 'layoutsDir' => APP_ROOT . '/app/layouts/',
* 'partialsDir' => APP_ROOT . '/app/layouts/partials/',
* ...
* )
* )
* ...
* </code>
* Usage:
*
* - Global partial
* <code>
* {{ partial('header/navigation') }} # goes to APP_ROOT/app/layouts/partials/header/navigation.volt
* </code>
* - Local partial in module Test, controller Index (app/modules/Test/views/index/)
* <code>
* {{ partial('./content/heading') }} # goes to APP_ROOT/app/modules/Test/views/index/partials/content/heading.volt
* </code>
*
* NOTE
* name of 'partial' directory inside of module must be the same as name of global 'partial' directory:
* APP_ROOT/app/layouts/partials => ../Test/views/index/partials
*
* @param string $partialPath
* @param null $params
* @return string|void
*/
public function partial($partialPath, $params = null)
{
if (strpos($partialPath, './') === 0) {
$viewPartialsDir = sprintf('%s%s%s%s',
dirname($this->view->getActiveRenderPath()),
DIRECTORY_SEPARATOR,
basename($this->view->getPartialsDir()),
DIRECTORY_SEPARATOR
);
} else {
$viewPartialsDir = $this->view->getPartialsDir();
}
$this->render($viewPartialsDir . $partialPath . $this->extension, $params);
}
}
27 changes: 3 additions & 24 deletions tests/DI/Service/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,16 @@ public function testRender()

$this->assertInstanceOf('\Phalcon\DI', $component->getDI());

$rendered = '';
ob_start();
$component->render(array('foo' => 'bar', 'baz' => 123));

$rendered = ob_get_contents();
ob_clean();

$this->assertEquals('bar123', $rendered);
$this->assertEquals('bar123', $component->render(array('foo' => 'bar', 'baz' => 123)));

$renderer = new Renderer($this->di->get('view'));
$component->setRenderer($renderer);

$rendered = '';
ob_start();
$component->render(array('foo' => 'bar', 'baz' => 123));

$rendered = ob_get_contents();
ob_clean();

$this->assertEquals('bar123', $rendered);
$this->assertEquals('bar123', $component->render(array('foo' => 'bar', 'baz' => 123)));

$component = new Fake($renderer);
$component->setDI($this->di);

$rendered = '';
ob_start();
$component->render(array('foo' => 'bar', 'baz' => 123));

$rendered = ob_get_contents();
ob_clean();

$this->assertEquals('bar123', $rendered);
$this->assertEquals('bar123', $component->render(array('foo' => 'bar', 'baz' => 123)));
}
}
Loading