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

Commit 9b78ca2

Browse files
committed
Merge branch 'develop'
Merged develop to master in preparation for 0.3.0 release.
2 parents cafa902 + 7c998ab commit 9b78ca2

24 files changed

+1652
-6
lines changed

CHANGELOG.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 0.2.2 - TBD
5+
## 0.3.0 - 2015-09-12
66

77
### Added
88

9-
- Nothing.
9+
- [#128](https://github.com/zendframework/zend-expressive/pull/128) adds
10+
container factories for each supported template implementation:
11+
- `Zend\Expressive\Container\Template\PlatesFactory`
12+
- `Zend\Expressive\Container\Template\TwigFactory`
13+
- `Zend\Expressive\Container\Template\ZendViewFactory`
14+
- [#128](https://github.com/zendframework/zend-expressive/pull/128) adds
15+
custom `url` and `serverUrl` zend-view helper implementations, to allow
16+
integration with any router and with PSR-7 URI instances. The newly
17+
added `ZendViewFactory` will inject these into the `HelperPluginManager` by
18+
default.
1019

1120
### Deprecated
1221

@@ -18,7 +27,9 @@ All notable changes to this project will be documented in this file, in reverse
1827

1928
### Fixed
2029

21-
- Nothing.
30+
- [#128](https://github.com/zendframework/zend-expressive/pull/128) fixes an
31+
expectation in the `WhoopsErrorHandler` tests to ensure the tests can run
32+
successfully.
2233

2334
## 0.2.1 - 2015-09-10
2435

doc/book/container/factories.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,107 @@ It consumes the following `config` structure:
203203

204204
The `editor` value must be a known editor name (see the Whoops documentation for
205205
pre-configured editor types), a callable, or a service name to use.
206+
207+
## PlatesFactory
208+
209+
- **Provides**: `Zend\Expressive\Template\Plates`
210+
- **FactoryName**: `Zend\Expressive\Container\Template\PlatesFactory`
211+
- **Suggested Name**: `Zend\Expressive\Template\TemplateInterface`
212+
- **Requires**: no additional services are required.
213+
- **Optional**:
214+
- `config`, an array or `ArrayAccess` instance. This will be used to further
215+
configure the `Plates` instance, specifically with the filename extension
216+
to use, and paths to inject.
217+
218+
It consumes the following `config` structure:
219+
220+
```php
221+
'templates' => [
222+
'extension' => 'file extension used by templates; defaults to html',
223+
'paths' => [
224+
// namespace / path pairs
225+
//
226+
// Numeric namespaces imply the default/main namespace. Paths may be
227+
// strings or arrays of string paths to associate with the namespace.
228+
],
229+
]
230+
```
231+
232+
One note: Due to a limitation in the Plates engine, you can only map one path
233+
per namespace when using Plates.
234+
235+
## TwigFactory
236+
237+
- **Provides**: `Zend\Expressive\Template\Twig`
238+
- **FactoryName**: `Zend\Expressive\Container\Template\TwigFactory`
239+
- **Suggested Name**: `Zend\Expressive\Template\TemplateInterface`
240+
- **Requires**: no additional services are required.
241+
- **Optional**:
242+
- `Zend\Expressive\Router\RouterInterface`; if found, it will be used to
243+
seed a `Zend\Expressive\Template\Twig\TwigExtension` instance for purposes
244+
of rendering application URLs.
245+
- `config`, an array or `ArrayAccess` instance. This will be used to further
246+
configure the `Twig` instance, specifically with the filename extension,
247+
paths to assets (and default asset version to use), and template paths to
248+
inject.
249+
250+
It consumes the following `config` structure:
251+
252+
```php
253+
'debug' => boolean,
254+
'templates' => [
255+
'cache_dir' => 'path to cached templates',
256+
'assets_url' => 'base URL for assets',
257+
'assets_version' => 'base version for assets',
258+
'extension' => 'file extension used by templates; defaults to html.twig',
259+
'paths' => [
260+
// namespace / path pairs
261+
//
262+
// Numeric namespaces imply the default/main namespace. Paths may be
263+
// strings or arrays of string paths to associate with the namespace.
264+
],
265+
]
266+
```
267+
268+
Whe `debug` is true, it disables caching, enables debug mode, enables strict
269+
variables, and enables auto reloading. The `assets_*` values are used to seed
270+
the `TwigExtension` instance (assuming the router was found).
271+
272+
## ZendViewFactory
273+
274+
- **Provides**: `Zend\Expressive\Template\ZendView`
275+
- **FactoryName**: `Zend\Expressive\Container\Template\ZendViewFactory`
276+
- **Suggested Name**: `Zend\Expressive\Template\TemplateInterface`
277+
- **Requires**: no additional services are required.
278+
- `Zend\Expressive\Router\RouterInterface`, in order to inject the custom
279+
url helper implementation.
280+
- **Optional**:
281+
- `config`, an array or `ArrayAccess` instance. This will be used to further
282+
configure the `ZendView` instance, specifically with the layout template
283+
name, entries for a `TemplateMapResolver`, and and template paths to
284+
inject.
285+
- `Zend\View\HelperPluginManager`; if present, will be used to inject the
286+
`PhpRenderer` instance.
287+
288+
It consumes the following `config` structure:
289+
290+
```php
291+
'templates' => [
292+
'layout' => 'name of layout view to use, if any',
293+
'map' => [
294+
// template => filename pairs
295+
],
296+
'paths' => [
297+
// namespace / path pairs
298+
//
299+
// Numeric namespaces imply the default/main namespace. Paths may be
300+
// strings or arrays of string paths to associate with the namespace.
301+
],
302+
]
303+
```
304+
305+
When creating the `PhpRenderer` instance, it will inject it with a
306+
`Zend\View\HelperPluginManager` instance (either pulled from the container, or
307+
instantiated directly). It injects the helper plugin manager with custom url and
308+
serverurl helpers, `Zend\Expressive\Template\ZendView\UrlHelper` and
309+
`Zend\Expressive\Template\ZendView\ServerUrlHelper`, respetively.

doc/book/template/intro.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ We currently support:
2828
- [Plates](plates.md)
2929
- [Twig](twig.md)
3030
- [zend-view](zend-view.md)
31+
32+
Each has an associated container factory; details are found in the
33+
[factories documentation](../container/factories.md).

doc/book/template/zend-view.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,28 @@ $content = $templates->render('blog/entry', [
145145
]);
146146
```
147147

148+
## Helpers
149+
150+
Expressive provides overrides of specific view helpers in order to better
151+
integrate with PSR-7. These include:
152+
153+
- `Zend\Expressive\Template\ZendView\UrlHelper`. This helper consumes the
154+
application's `Zend\Expressive\Router\RouterInterface` instance in order
155+
to generate URIs. It's signature is:
156+
`url($routeName, array $substitutions = [])`
157+
- `Zend\Expressive\Template\ZendView\ServerUrlHelper`. This helper consumes the
158+
URI from the application's request in order to provide fully qualified URIs.
159+
It's signature is: `serverUrl($path = null)`.
160+
161+
To use this particular helper, you will need to inject it with the request URI
162+
somewhere within your application:
163+
164+
```php
165+
$serverUrlHelper->setUri($request->getUri());
166+
```
167+
168+
We recommend doing this within a pre-pipeline middleware.
169+
148170
## Recommendations
149171

150172
We recommend the following practices when using the zend-view adapter:
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.twig',
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.twig');
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+
}

0 commit comments

Comments
 (0)