Move Smarty view creation to ViewFactory class

This commit is contained in:
Pierre Rudloff 2017-04-26 00:50:19 +02:00
parent 4d104c852f
commit 0e7aaea9fc
3 changed files with 47 additions and 23 deletions

41
classes/ViewFactory.php Normal file
View file

@ -0,0 +1,41 @@
<?php
/**
* ViewFactory class.
*/
namespace Alltube;
use Slim\Container;
use Slim\Http\Request;
use Slim\Views\Smarty;
use Slim\Views\SmartyPlugins;
/**
* Create Smarty view object.
*/
class ViewFactory
{
/**
* Create Smarty view object.
*
* @param Container $container Slim dependency container
* @param Request $request PSR-7 request
*
* @return Smarty
*/
public static function create(Container $container, Request $request = null)
{
if (!isset($request)) {
$request = $container['request'];
}
$view = new Smarty(__DIR__.'/../templates/');
$smartyPlugins = new SmartyPlugins($container['router'], $request->getUri());
$view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']);
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
$view->registerPlugin('modifier', 'noscheme', 'Smarty_Modifier_noscheme');
return $view;
}
}

View file

@ -4,29 +4,21 @@ require_once __DIR__.'/vendor/autoload.php';
use Alltube\Config; use Alltube\Config;
use Alltube\Controller\FrontController; use Alltube\Controller\FrontController;
use Alltube\UglyRouter; use Alltube\UglyRouter;
use Alltube\ViewFactory;
use Slim\App;
if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/index.php') !== false) { if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/index.php') !== false) {
header('Location: '.str_ireplace('/index.php', '/', $_SERVER['REQUEST_URI'])); header('Location: '.str_ireplace('/index.php', '/', $_SERVER['REQUEST_URI']));
die; die;
} }
$app = new \Slim\App(); $app = new App();
$container = $app->getContainer(); $container = $app->getContainer();
$config = Config::getInstance(); $config = Config::getInstance();
if ($config->uglyUrls) { if ($config->uglyUrls) {
$container['router'] = new UglyRouter(); $container['router'] = new UglyRouter();
} }
$container['view'] = function ($c) { $container['view'] = ViewFactory::create($container);
$view = new \Slim\Views\Smarty(__DIR__.'/templates/');
$smartyPlugins = new \Slim\Views\SmartyPlugins($c['router'], $c['request']->getUri());
$view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']);
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
$view->registerPlugin('modifier', 'noscheme', 'Smarty_Modifier_noscheme');
return $view;
};
$controller = new FrontController($container, null, $_COOKIE); $controller = new FrontController($container, null, $_COOKIE);

View file

@ -7,6 +7,7 @@ namespace Alltube\Test;
use Alltube\Config; use Alltube\Config;
use Alltube\Controller\FrontController; use Alltube\Controller\FrontController;
use Alltube\ViewFactory;
use Slim\Container; use Slim\Container;
use Slim\Http\Environment; use Slim\Http\Environment;
use Slim\Http\Request; use Slim\Http\Request;
@ -53,17 +54,7 @@ class FrontControllerTest extends \PHPUnit_Framework_TestCase
$this->container = new Container(); $this->container = new Container();
$this->request = Request::createFromEnvironment(Environment::mock()); $this->request = Request::createFromEnvironment(Environment::mock());
$this->response = new Response(); $this->response = new Response();
$this->container['view'] = function ($c) { $this->container['view'] = ViewFactory::create($this->container, $this->request);
$view = new \Slim\Views\Smarty(__DIR__.'/../templates/');
$smartyPlugins = new \Slim\Views\SmartyPlugins($c['router'], $this->request->getUri());
$view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']);
$view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);
$view->registerPlugin('modifier', 'noscheme', 'Smarty_Modifier_noscheme');
return $view;
};
$this->controller = new FrontController($this->container, Config::getInstance('config_test.yml')); $this->controller = new FrontController($this->container, Config::getInstance('config_test.yml'));
$this->container['router']->map(['GET'], '/', [$this->controller, 'index']) $this->container['router']->map(['GET'], '/', [$this->controller, 'index'])
->setName('index'); ->setName('index');