Skip to content

Commit

Permalink
Merge branch 'trunk' of git://github.com/integry/integry-framework in…
Browse files Browse the repository at this point in the history
…to trunk
  • Loading branch information
integry committed Oct 1, 2011
2 parents 431de15 + 65ad0b3 commit ee4744a
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 5 deletions.
13 changes: 10 additions & 3 deletions Application.php
Expand Up @@ -43,6 +43,7 @@ class Application
{
protected $routerClass = 'Router';
protected $requestClass = 'Request';
protected $rendererClass = 'PHPRenderer';

/**
* @var Router
Expand Down Expand Up @@ -114,6 +115,11 @@ public function setRenderer(Renderer $renderer)
$this->renderer = $renderer;
}

public function setRendererClass($className)
{
$this->rendererClass = $className;
}

/**
* Gets renderer for application
*
Expand All @@ -123,7 +129,8 @@ public function getRenderer()
{
if (is_null($this->renderer))
{
$this->renderer = new SmartyRenderer($this);
$class = $this->rendererClass;
$this->renderer = new $class($this);
}
return $this->renderer;
}
Expand Down Expand Up @@ -468,7 +475,7 @@ public function getView($controllerName, $actionName)
{
$controllerName = str_replace('\\', '/', $controllerName);
$dir = dirname($this->controllerDirectories[str_replace('/', '.', $controllerName)]) . '/view';
return $dir . '/' . str_replace('.', '/', $controllerName) . '/' . $actionName . '.tpl';
return $dir . '/' . str_replace('.', '/', $controllerName) . '/' . $actionName . '.' . $this->getRenderer()->getViewExtension();
}

/**
Expand All @@ -479,7 +486,7 @@ public function getView($controllerName, $actionName)
*/
public function getLayoutPath($layout)
{
return ClassLoader::getRealPath('application.view.layout.' . $layout) . '.tpl';
return ClassLoader::getRealPath('application.view.layout.' . $layout) . '.' . $this->getRenderer()->getViewExtension();
}

public function __get($name)
Expand Down
45 changes: 45 additions & 0 deletions Initialize.php
@@ -0,0 +1,45 @@
<?php

/**
* Integry framework bootstrap
*
* @package framework
* @author Integry Systems
*
*/

require_once(dirname(__file__) . DIRECTORY_SEPARATOR . 'ClassLoader.php');

// we're assuming that application root is one level above the framework directory
// if it's not the case, call ClassLoader::mountPath('.', '/path/to/the/root/directory');
ClassLoader::mountPath('.', dirname(dirname(__file__)) . DIRECTORY_SEPARATOR);

ClassLoader::mountPath('framework', dirname(__file__) . DIRECTORY_SEPARATOR);

ClassLoader::import('framework.request.*');
ClassLoader::import('framework.renderer.*');
ClassLoader::import('framework.response.*');
ClassLoader::import('framework.controller.*');

ClassLoader::import('framework.Application');

$app = new Application();

// initialize default routing rules
$router = $app->getRouter();

$rules = array(
array(":controller", array("action" => "index"), array()),
array(":controller/:id", array("action" => "index"), array("id" => "-?[0-9]+")),
array(":controller/:action", array(), array()),
array(":controller/:action/:id", array(), array("id" => "-?[0-9]+")),
);

foreach ($rules as $route)
{
$router->connect($route[0], $route[1], $route[2]);
}

return $app;

?>
6 changes: 4 additions & 2 deletions controller/Controller.php
Expand Up @@ -288,14 +288,16 @@ public final function addBlock($containerName, $blockName, $viewName = null, $pr
$blockName = array($this, $blockName);
}

$viewExt = $this->application->getRenderer()->getViewExtension();

$blockMethodName = $blockName[1] . (substr($blockName[1], -5) == 'Block' ? '' : 'Block');

if (empty($viewName))
{
$viewName = "block" . DIRECTORY_SEPARATOR . $blockName[1] . ".tpl";
$viewName = "block" . DIRECTORY_SEPARATOR . $blockName[1] . "." . $viewExt;
}

$viewPath = $viewName . (substr($viewName, -4) == '.tpl' ? '' : '.tpl');
$viewPath = $viewName . (substr($viewName, -1 * (strlen($viewExt) + 1)) == '.' . $viewExt ? '' : '.' . $viewExt);

/*
if (!method_exists($blockName[0], $blockMethodName))
Expand Down
83 changes: 83 additions & 0 deletions renderer/PHPRenderer.php
@@ -0,0 +1,83 @@
<?php

ClassLoader::import('framework.renderer.Renderer');

/**
* Renderer implementation based on raw PHP files ("PHP itself is a templating engine")
*
* @package framework.renderer
* @author Integry Systems
*/
class PHPRenderer extends Renderer
{
protected $values = array();

public function __construct(Application $application)
{
$this->application = $application;
}

/**
* Gets application instance
*
* @return Smarty
*/
public function getApplication()
{
return $this->application;
}

public function set($name, $value)
{
$this->values[$name] = $value;
}

public function appendValue($name, $value)
{
$this->values[$name] = $value;
}

public function setValueArray($array)
{
$this->values[$name] = $array;
}

public function setObject($name, $object)
{
$this->values[$name] = $object;
}

public function unsetValue($name)
{
unset($this->values[$name]);
}

public function unsetAll()
{
$this->values = array();
}

public function render($view)
{
if (file_exists($view))
{
ob_start();
extract($this->values);
include $view;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
else
{
throw new ViewNotFoundException($view);
}
}

public function getViewExtension()
{
return 'php';
}
}

?>
7 changes: 7 additions & 0 deletions renderer/Renderer.php
Expand Up @@ -91,6 +91,13 @@ public function process(Renderable $object, $view)
* @throws ViewNotFoundException if view does not exists
*/
abstract public function render($view);

/**
* Returns the file extension of view files
*
* @return string File extension
*/
abstract public function getViewExtension();
}

?>
4 changes: 4 additions & 0 deletions renderer/SmartyRenderer.php
Expand Up @@ -173,6 +173,10 @@ public static function registerHelperDirectory($directory)
self::$helperDirectories[] = $directory;
}

public function getViewExtension()
{
return 'tpl';
}
}

?>

0 comments on commit ee4744a

Please sign in to comment.