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

Commit 06c04e4

Browse files
committed
Update component to use zend-expressive-helpers
This patch completely changes how the `url` and `serverUrl` helpers are created and registered with the helper manager: - `UrlHelper` and `ServerUrlHelper` are now proxies for their equivalents from zend-expressive-helpers, ensuring they can be used as zend-view helpers. - `UrlHelperFactory` was now no longer necessary, as it existed previously to ensure the helper was injected with a `RouterInterface`. - `ApplicationUrlDelegatorFactory` was no longer necessary, as the zend-expressive-helpers `UrlHelperFactory` takes care of registering the instance it creates as a route result observer. - `ZendViewRendererFactory` was updated to pull the zend-expressive-helpers `UrlHelper` and `ServerUrlHelper` instances and return their decorated instances when creating factories for the `url` and `serverUrl` helpers, respectively. Essentially, this approach simplifies the component significantly, and moves the logic for URL generation to another component entirely.
1 parent 60abfad commit 06c04e4

15 files changed

+177
-571
lines changed

CHANGELOG.md

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,42 @@
22

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

5-
## 0.3.2 - TBD
5+
## 0.4.0 - 2015-12-04
66

77
### Added
88

9-
- Nothing.
9+
- [#11](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/11)
10+
adds a factory for providing the `HelperPluginManager`, and support in the
11+
`ZendViewRendererFactory` for injecting the `HelperPluginManager` service
12+
(using its FQCN) instead of instantiating one directly.
13+
- [#13](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/13)
14+
adds `zendframework/zend-expressive-helpers` as a dependency, in order to
15+
consume its `UrlHelper` and `ServerUrlHelper` implementations.
1016

1117
### Deprecated
1218

1319
- Nothing.
1420

1521
### Removed
1622

17-
- Nothing.
23+
- [#13](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/13)
24+
removes the `UrlHelperFactory`.
25+
- [#13](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/13)
26+
removes the `Zend\Expressive\ZendView\ApplicationUrlDelegatorFactory`. This
27+
functionality is obsolete due to the changes made to the `UrlHelper` in this
28+
release.
1829

1930
### Fixed
2031

21-
- Nothing.
32+
- [#13](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/13)
33+
updates the `UrlHelper` to be a proxy to `Zend\Expressive\Helper\UrlHelper`.
34+
- [#13](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/13)
35+
updates the `ServerUrlHelper` to be a proxy to `Zend\Expressive\Helper\ServerUrlHelper`.
36+
- [#13](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/13)
37+
modifies the logic for injecting the `url` and `serverurl` helpers to pull the
38+
`Zend\Expressive\Helper\UrlHelper` and `Zend\Expressive\Helper\ServerUrlHelper`
39+
instances, respectively, to inject into the package's own `UrlHelper` and
40+
`ServerUrlHelper` instances.
2241

2342
## 0.3.1 - 2015-12-03
2443

@@ -47,9 +66,9 @@ All notable changes to this project will be documented in this file, in reverse
4766

4867
### Added
4968

50-
- [#4](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/)
69+
- [#4](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/4)
5170
Allow rendering view models via render
52-
- [#9](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/)
71+
- [#9](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/9)
5372
updates `UrlHelper` to implement `Zend\Expressive\Template\RouteResultObserverInterface`,
5473
and the `update()` method it defines. This allows it to observer the
5574
application for the `RouteResult` and store it for later URI generation.

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,29 @@ can recommend the following implementations:
2323
`composer require mouf/pimple-interop`
2424
- [Aura.Di](https://github.com/auraphp/Aura.Di)
2525

26+
## View Helpers
27+
28+
To use view helpers, the `ZendViewRendererFactory`:
29+
30+
- requires a `config` service; with
31+
- a `view_helpers` sub-key; which
32+
- follows standard zend-servicemanager configuration.
33+
34+
To use the `UrlHelper` provided in this package, ensure that you register its
35+
factory in that configuration:
36+
37+
```php
38+
use Zend\Expressive\ZendView\UrlHelperFactory;
39+
40+
return [
41+
'view_helpers' => [
42+
'factories' => [
43+
'url' => UrlHelperFactory::class,
44+
],
45+
],
46+
];
47+
```
48+
2649
## Documentation
2750

2851
See the [zend-expressive](https://github.com/zendframework/zend-expressive/blob/master/doc/book)

composer.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"php": "^5.5 || ^7.0",
2121
"container-interop/container-interop": "^1.1",
2222
"psr/http-message": "^1.0",
23-
"zendframework/zend-expressive-router": "^1.0",
23+
"zendframework/zend-expressive-helpers": "^1.0",
2424
"zendframework/zend-expressive-template": "^1.0.1",
2525
"zendframework/zend-filter": "^2.5",
2626
"zendframework/zend-i18n": "^2.5",
@@ -30,7 +30,6 @@
3030
"require-dev": {
3131
"phpunit/phpunit": "^4.7",
3232
"squizlabs/php_codesniffer": "^2.3",
33-
"zendframework/zend-diactoros": "^1.2",
3433
"zendframework/zend-expressive": "~1.0.0-dev@dev || ^1.0"
3534
},
3635
"autoload": {
@@ -43,11 +42,6 @@
4342
"ZendTest\\Expressive\\ZendView\\": "test/"
4443
}
4544
},
46-
"suggest": {
47-
"mouf/pimple-interop": "^1.0 to use Pimple for dependency injection",
48-
"aura/di": "3.0.*@beta to make use of Aura.Di dependency injection container",
49-
"zendframework/zend-expressive": "^1.0 if you wish to use the ApplicationUrlDelegatorFactory"
50-
},
5145
"scripts": {
5246
"check": [
5347
"@cs",

src/ApplicationUrlDelegatorFactory.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/Exception/ExceptionInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* @see http://github.com/zendframework/zend-expressive for the canonical source repository
4+
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace Zend\Expressive\ZendView\Exception;
9+
10+
interface ExceptionInterface
11+
{
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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\ZendView\Exception;
11+
12+
use DomainException;
13+
use Interop\Container\Exception\ContainerException;
14+
15+
class MissingHelperException extends DomainException implements
16+
ContainerException,
17+
ExceptionInterface
18+
{
19+
}

src/HelperPluginManagerFactory.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
<?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+
*/
29

310
namespace Zend\Expressive\ZendView;
411

@@ -14,7 +21,6 @@ public function __invoke(ContainerInterface $container)
1421
$config = isset($config['view_helpers']) ? $config['view_helpers'] : [];
1522
$manager = new HelperPluginManager(new Config($config));
1623
$manager->setServiceLocator($container);
17-
1824
return $manager;
1925
}
2026
}

src/ServerUrlHelper.php

Lines changed: 15 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Zend\Expressive\ZendView;
99

1010
use Psr\Http\Message\UriInterface;
11+
use Zend\Expressive\Helper\ServerUrlHelper as BaseHelper;
1112
use Zend\View\Helper\AbstractHelper;
1213

1314
/**
@@ -16,91 +17,37 @@
1617
class ServerUrlHelper extends AbstractHelper
1718
{
1819
/**
19-
* @var UriInterface
20+
* @var BaseHelper
2021
*/
21-
private $uri;
22+
private $helper;
23+
24+
/**
25+
* @param BaseHelper $helper
26+
*/
27+
public function __construct(BaseHelper $helper)
28+
{
29+
$this->helper = $helper;
30+
}
2231

2332
/**
2433
* Return a path relative to the current request URI.
2534
*
26-
* If no request URI has been injected, it returns an absolute path
27-
* only; relative paths are made absolute, and absolute paths are returned
28-
* verbatim (null paths are returned as root paths).
29-
*
30-
* Otherwise, returns a fully-qualified URI based on the injected request
31-
* URI; absolute paths replace the request URI path, while relative paths
32-
* are appended to it (and null paths are considered the current path).
33-
*
34-
* The $path may optionally contain the query string and/or fragment to
35-
* use.
35+
* Proxies to `Zend\Expressive\Helper\ServerUrlHelper::generate()`.
3636
*
3737
* @param null|string $path
3838
* @return string
3939
*/
4040
public function __invoke($path = null)
4141
{
42-
if ($this->uri instanceof UriInterface) {
43-
return $this->createUrlFromUri($path);
44-
}
45-
46-
if (empty($path)) {
47-
return '/';
48-
}
49-
50-
if ('/' === $path[0]) {
51-
return $path;
52-
}
53-
54-
return '/' . $path;
42+
return $this->helper->generate($path);
5543
}
5644

5745
/**
46+
* Proxies to `Zend\Expressive\Helper\ServerUrlHelper::setUri()`
5847
* @param UriInterface $uri
5948
*/
6049
public function setUri(UriInterface $uri)
6150
{
62-
$this->uri = $uri;
63-
}
64-
65-
/**
66-
* @param string $specification
67-
* @return string
68-
*/
69-
private function createUrlFromUri($specification)
70-
{
71-
preg_match(
72-
'%^(?P<path>[^?#]*)(?:(?:\?(?P<query>[^#]*))?(?:\#(?P<fragment>.*))?)$%',
73-
(string) $specification,
74-
$matches
75-
);
76-
$path = $matches['path'];
77-
$query = isset($matches['query']) ? $matches['query'] : '';
78-
$fragment = isset($matches['fragment']) ? $matches['fragment'] : '';
79-
80-
$uri = $this->uri
81-
->withQuery('')
82-
->withFragment('');
83-
84-
// Relative path
85-
if (! empty($path) && '/' !== $path[0]) {
86-
$path = rtrim($this->uri->getPath(), '/') . '/' . $path;
87-
}
88-
89-
// Path present; set on URI
90-
if (! empty($path)) {
91-
$uri = $uri->withPath($path);
92-
}
93-
94-
// Query present; set on URI
95-
if (! empty($query)) {
96-
$uri = $uri->withQuery($query);
97-
}
98-
99-
// Fragment present; set on URI
100-
if (! empty($fragment)) {
101-
$uri = $uri->withFragment($fragment);
102-
}
103-
104-
return (string) $uri;
51+
$this->helper->setUri($uri);
10552
}
10653
}

0 commit comments

Comments
 (0)