Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 2f29555

Browse files
committed
[#122] Import factories from expressive-composer-installer
This patch imports the factories introduced at: - https://github.com/xtreamwayz/expressive-composer-installer/tree/master/src/App/Template for each of Twig, Plates, and ZendView, with modifications (particularly the lack of default template paths). Additionally, unit tests have been added to verify behavior of each container. The Twig support now includes the TwigExtension, which overrides the path and asset twig helpers in order to delegate to zend-expressive functionality.
1 parent e24fc54 commit 2f29555

File tree

15 files changed

+967
-0
lines changed

15 files changed

+967
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
6+
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
8+
*/
9+
10+
namespace Zend\Expressive\Container\Template;
11+
12+
use Interop\Container\ContainerInterface;
13+
use League\Plates\Engine as PlatesEngine;
14+
use Zend\Expressive\Template\Plates;
15+
16+
/**
17+
* Create and return a Plates template instance.
18+
*
19+
* Optionally uses the service 'config', which should return an array. This
20+
* factory consumes the following structure:
21+
*
22+
* <code>
23+
* 'templates' => [
24+
* 'extension' => 'file extension used by templates; defaults to html',
25+
* 'paths' => [
26+
* // namespace / path pairs
27+
* //
28+
* // Numeric namespaces imply the default/main namespace. Paths may be
29+
* // strings or arrays of string paths to associate with the namespace.
30+
* ],
31+
* ]
32+
* </code>
33+
*/
34+
class PlatesFactory
35+
{
36+
/**
37+
* @param ContainerInterface $container
38+
* @return Plates
39+
*/
40+
public function __invoke(ContainerInterface $container)
41+
{
42+
$config = $container->has('config') ? $container->get('config') : [];
43+
$config = isset($config['templates']) ? $config['templates'] : [];
44+
45+
// Create the engine instance:
46+
$engine = new PlatesEngine();
47+
48+
// Set file extension
49+
if (isset($config['extension'])) {
50+
$engine->setFileExtension($config['extension']);
51+
}
52+
53+
// Inject engine
54+
$plates = new Plates($engine);
55+
56+
// Add template paths
57+
$allPaths = isset($config['paths']) && is_array($config['paths']) ? $config['paths'] : [];
58+
foreach ($allPaths as $namespace => $paths) {
59+
$namespace = is_numeric($namespace) ? null : $namespace;
60+
foreach ((array) $paths as $path) {
61+
$plates->addPath($path, $namespace);
62+
}
63+
}
64+
65+
return $plates;
66+
}
67+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
6+
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
8+
*/
9+
10+
namespace Zend\Expressive\Container\Template;
11+
12+
use Interop\Container\ContainerInterface;
13+
use Twig_Environment as TwigEnvironment;
14+
use Twig_Extension_Debug as TwigExtensionDebug;
15+
use Twig_Loader_Filesystem as TwigLoader;
16+
use Zend\Expressive\Router\RouterInterface;
17+
use Zend\Expressive\Template\Twig;
18+
19+
/**
20+
* Create and return a Twig template instance.
21+
*
22+
* Optionally uses the service 'config', which should return an array. This
23+
* factory consumes the following structure:
24+
*
25+
* <code>
26+
* 'debug' => boolean,
27+
* 'templates' => [
28+
* 'cache_dir' => 'path to cached templates',
29+
* 'assets_url' => 'base URL for assets',
30+
* 'assets_version' => 'base version for assets',
31+
* 'extension' => 'file extension used by templates; defaults to html',
32+
* 'paths' => [
33+
* // namespace / path pairs
34+
* //
35+
* // Numeric namespaces imply the default/main namespace. Paths may be
36+
* // strings or arrays of string paths to associate with the namespace.
37+
* ],
38+
* ]
39+
* </code>
40+
*/
41+
class TwigFactory
42+
{
43+
/**
44+
* @param ContainerInterface $container
45+
* @return Twig
46+
*/
47+
public function __invoke(ContainerInterface $container)
48+
{
49+
$config = $container->has('config') ? $container->get('config') : [];
50+
$debug = array_key_exists('debug', $config) ? (bool) $config['debug'] : false;
51+
$config = isset($config['templates']) ? $config['templates'] : [];
52+
$cacheDir = isset($config['cache_dir']) ? $config['cache_dir'] : false;
53+
54+
// Create the engine instance
55+
$loader = new TwigLoader();
56+
$environment = new TwigEnvironment($loader, [
57+
'cache' => $debug ? false : $cacheDir,
58+
'debug' => $debug,
59+
'strict_variables' => $debug,
60+
'auto_reload' => $debug
61+
]);
62+
63+
// Add extensions
64+
if ($container->has(RouterInterface::class)) {
65+
$environment->addExtension(new Twig\TwigExtension(
66+
$container->get(RouterInterface::class),
67+
isset($config['assets_url']) ? $config['assets_url'] : '',
68+
isset($config['assets_version']) ? $config['assets_version'] : ''
69+
));
70+
}
71+
72+
if ($debug) {
73+
$environment->addExtension(new TwigExtensionDebug());
74+
}
75+
76+
// Inject environment
77+
$twig = new Twig($environment, isset($config['extension']) ? $config['extension'] : 'html');
78+
79+
// Add template paths
80+
$allPaths = isset($config['paths']) && is_array($config['paths']) ? $config['paths'] : [];
81+
foreach ($allPaths as $namespace => $paths) {
82+
$namespace = is_numeric($namespace) ? null : $namespace;
83+
foreach ((array) $paths as $path) {
84+
$twig->addPath($path, $namespace);
85+
}
86+
}
87+
88+
return $twig;
89+
}
90+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
6+
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
8+
*/
9+
10+
namespace Zend\Expressive\Container\Template;
11+
12+
use Interop\Container\ContainerInterface;
13+
use Zend\Expressive\Template\ZendView;
14+
use Zend\View\Renderer\PhpRenderer;
15+
use Zend\View\Resolver;
16+
17+
/**
18+
* Create and return a ZendView template instance.
19+
*
20+
* Optionally uses the service 'config', which should return an array. This
21+
* factory consumes the following structure:
22+
*
23+
* <code>
24+
* 'templates' => [
25+
* 'layout' => 'name of layout view to use, if any',
26+
* 'map' => [
27+
* // template => filename pairs
28+
* ],
29+
* 'paths' => [
30+
* // namespace / path pairs
31+
* //
32+
* // Numeric namespaces imply the default/main namespace. Paths may be
33+
* // strings or arrays of string paths to associate with the namespace.
34+
* ],
35+
* ]
36+
* </code>
37+
*/
38+
class ZendViewFactory
39+
{
40+
/**
41+
* @param ContainerInterface $container
42+
* @returns ZendView
43+
*/
44+
public function __invoke(ContainerInterface $container)
45+
{
46+
$config = $container->has('config') ? $container->get('config') : [];
47+
$config = isset($config['templates']) ? $config['templates'] : [];
48+
49+
// Configuration
50+
$resolver = new Resolver\AggregateResolver();
51+
$resolver->attach(
52+
new Resolver\TemplateMapResolver(isset($config['map']) ? $config['map'] : []),
53+
100
54+
);
55+
56+
// Create the renderer
57+
$renderer = new PhpRenderer();
58+
$renderer->setResolver($resolver);
59+
60+
// Inject renderer
61+
$view = new ZendView($renderer, isset($config['layout']) ? $config['layout'] : null);
62+
63+
// Add template paths
64+
$allPaths = isset($config['paths']) && is_array($config['paths']) ? $config['paths'] : [];
65+
foreach ($allPaths as $namespace => $paths) {
66+
$namespace = is_numeric($namespace) ? null : $namespace;
67+
foreach ((array) $paths as $path) {
68+
$view->addPath($path, $namespace);
69+
}
70+
}
71+
72+
return $view;
73+
}
74+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
6+
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
8+
*/
9+
10+
namespace Zend\Expressive\Template\Twig;
11+
12+
use Twig_Extension;
13+
use Twig_SimpleFunction;
14+
use Zend\Expressive\Router\RouterInterface;
15+
16+
/**
17+
* Twig extension for rendering URLs and assets URLs from Expressive.
18+
*
19+
* @author Geert Eltink (https://xtreamwayz.github.io)
20+
*/
21+
class TwigExtension extends Twig_Extension
22+
{
23+
/**
24+
* @var RouterInterface
25+
*/
26+
private $router;
27+
28+
/**
29+
* @var string
30+
*/
31+
private $assetsUrl;
32+
33+
/**
34+
* @var string
35+
*/
36+
private $assetsVersion;
37+
38+
/**
39+
* @param RouterInterface $router
40+
* @param string $assetsUrl
41+
* @param string $assetsVersion
42+
*/
43+
public function __construct(
44+
RouterInterface $router,
45+
$assetsUrl,
46+
$assetsVersion
47+
) {
48+
$this->router = $router;
49+
$this->assetsUrl = $assetsUrl;
50+
$this->assetsVersion = $assetsVersion;
51+
}
52+
53+
/**
54+
* @return string
55+
*/
56+
public function getName()
57+
{
58+
return 'zend-expressive';
59+
}
60+
61+
/**
62+
* @return Twig_SimpleFunction[]
63+
*/
64+
public function getFunctions()
65+
{
66+
return [
67+
new Twig_SimpleFunction('path', [$this, 'renderUri']),
68+
new Twig_SimpleFunction('asset', [$this, 'renderAssetUrl']),
69+
];
70+
}
71+
72+
/**
73+
* Usage: {{ path('name', parameters) }}
74+
*
75+
* @param $name
76+
* @param array $parameters
77+
* @param bool $relative
78+
* @return string
79+
*/
80+
public function renderUri($name, $parameters = [], $relative = false)
81+
{
82+
return $this->router->generateUri($name, $parameters);
83+
}
84+
85+
/**
86+
* Usage: {{ asset('path/to/asset/name.ext', version=3) }}
87+
*
88+
* @param $path
89+
* @param null $packageName
90+
* @param bool $absolute
91+
* @param null $version
92+
* @return string
93+
*/
94+
public function renderAssetUrl($path, $packageName = null, $absolute = false, $version = null)
95+
{
96+
return sprintf(
97+
'%s%s?v=%s',
98+
$this->assetsUrl,
99+
$path,
100+
($version) ? $version : $this->assetsVersion
101+
);
102+
}
103+
}

0 commit comments

Comments
 (0)