Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit a1f2d6f

Browse files
committed
Merge pull request #1 from symfony-cmf/dev
Experimental builder chain idea
2 parents 579a31e + f5a9c60 commit a1f2d6f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1896
-1764
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ php:
66

77
env:
88
- SYMFONY_VERSION=2.1.*
9-
- SYMFONY_VERSION=dev-master
9+
- SYMFONY_VERSION=2.2.*
10+
- SYMFONY_VERSION=2.3.*
1011

1112
before_script:
1213
- composer install --dev

AutoRoute/AutoRouteManager.php

Lines changed: 11 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,20 @@
77
use Symfony\Cmf\Bundle\RoutingAutoRouteBundle\Document\AutoRoute;
88
use Symfony\Cmf\Bundle\CoreBundle\Slugifier\SlugifierInterface;
99
use PHPCR\Util\NodeHelper;
10+
use Doctrine\Common\Util\ClassUtils;
1011

1112
/**
1213
* This class is concerned with the automatic creation of route objects.
1314
*
1415
* @author Daniel Leech <[email protected]>
15-
* @author Sjoerd Peters <[email protected]>
1616
*/
1717
class AutoRouteManager
1818
{
19-
protected $dm;
20-
protected $phpcrSession;
21-
protected $mapping;
22-
protected $defaultPath;
23-
protected $slugifier;
19+
protected $bucf;
2420

25-
/**
26-
* @TODO: Should defaultPath be contained in a service to
27-
* enable this property to be modified at runtime?
28-
* @TODO: Replace Slugifier with TransformerFactory or similar.
29-
*
30-
* @param DocumentManager $dm PHPCR-ODM Document Manager
31-
* @param array $mapping Class => configuration mapping
32-
* @param SlugifierInterface $slugifier Slugifier
33-
* @param string $defaultPath Default base path for new routes
34-
*/
35-
public function __construct(
36-
DocumentManager $dm,
37-
array $mapping,
38-
SlugifierInterface $slugifier,
39-
$defaultPath
40-
)
21+
public function __construct(BuilderUnitChainFactory $bucf)
4122
{
42-
$this->dm = $dm;
43-
$this->mapping = $mapping;
44-
$this->slugifier = $slugifier;
45-
$this->defaultPath = $defaultPath;
46-
$this->phpcrSession = $dm->getPhpcrSession();
23+
$this->bucf = $bucf;
4724
}
4825

4926
/**
@@ -58,18 +35,13 @@ public function __construct(
5835
*/
5936
public function updateAutoRouteForDocument($document)
6037
{
61-
$metadata = $this->getMetadata($document);
62-
$autoRoute = $this->getAutoRouteForDocument($document);
63-
64-
$autoRouteName = $this->getRouteName($document);
65-
$autoRoute->setName($autoRouteName);
66-
67-
$autoRouteParent = $this->getParentRoute($document);
68-
$autoRoute->setParent($autoRouteParent);
38+
$context = new BuilderContext;
39+
$context->setObject($document);
6940

70-
$this->dm->persist($autoRoute);
41+
$builderUnitChain = $this->bucf->getChain(ClassUtils::getClass($document));
42+
$builderUnitChain->executeChain($context);
7143

72-
return $autoRoute;
44+
return $context;
7345
}
7446

7547
/**
@@ -83,12 +55,7 @@ public function updateAutoRouteForDocument($document)
8355
*/
8456
public function removeAutoRoutesForDocument($document)
8557
{
86-
$autoRoutes = $this->fetchAutoRoutesForDocument($document);
87-
foreach ($autoRoutes as $autoRoute) {
88-
$this->dm->remove($autoRoute);
89-
}
90-
91-
return $autoRoutes;
58+
throw new \Exception('Implement me??');
9259
}
9360

9461
/**
@@ -100,158 +67,6 @@ public function removeAutoRoutesForDocument($document)
10067
*/
10168
public function isAutoRouteable($document)
10269
{
103-
foreach ($this->mapping as $classFqn => $metadata) {
104-
if ($document instanceof $classFqn) {
105-
return true;
106-
}
107-
}
108-
109-
return false;
110-
}
111-
112-
/**
113-
* Generate a route name based on the designated route name method in
114-
* the given mapped document.
115-
*
116-
* Here we use the slugifier service given to this class to normalize
117-
* the title.
118-
*
119-
* @param object Mapped document
120-
*
121-
* @return string
122-
*/
123-
protected function getRouteName($document)
124-
{
125-
$metadata = $this->getMetadata($document);
126-
127-
$routeNameMethod = $metadata['route_method_name'];
128-
$routeName = $document->$routeNameMethod();
129-
$routeName = $this->slugifier->slugify($routeName);
130-
131-
return $routeName;
132-
}
133-
134-
/**
135-
* Return the parent route for the generated AutoRoute.
136-
*
137-
* Currently we check to see if a base route path has been specified
138-
* in the given mapped document, if not we fall back to the global default.
139-
*
140-
* @TODO: Enable dynamic parents (e.g. name-of-my-blog/my-post)
141-
*
142-
* @param object Get parent route of this mapped document.
143-
*
144-
* @return Route
145-
*/
146-
protected function getParentRoute($document)
147-
{
148-
$metadata = $this->getMetadata($document);
149-
$defaultPath = $metadata['base_path'] ? : $this->defaultPath;
150-
151-
if ($metadata['base_path_auto_create']) {
152-
if (!$this->phpcrSession->nodeExists($defaultPath)) {
153-
NodeHelper::createPath($this->phpcrSession, $defaultPath);
154-
}
155-
}
156-
157-
$parent = $this->dm->find(null, $defaultPath);
158-
159-
if (!$parent) {
160-
throw new \Exception(sprintf(
161-
'Could not find default route parent at path "%s"',
162-
$defaultPath
163-
));
164-
}
165-
166-
return $parent;
167-
}
168-
169-
/**
170-
* Convenience method for retrieving Metadata.
171-
*/
172-
protected function getMetadata($document)
173-
{
174-
foreach ($this->mapping as $classFqn => $metadata) {
175-
if ($document instanceof $classFqn) {
176-
return $metadata;
177-
}
178-
}
179-
}
180-
181-
/**
182-
* Return the existing or a new AutoRoute for the given document.
183-
*
184-
* @throws \Exception If we have more than one
185-
*
186-
* @param object $document Mapped document that needs an AutoRoute
187-
*
188-
* @return AutoRoute
189-
*/
190-
protected function getAutoRouteForDocument($document)
191-
{
192-
$autoRoutes = array();
193-
194-
if ($this->isDocumentPersisted($document)) {
195-
$autoRoutes = $this->fetchAutoRoutesForDocument($document);
196-
}
197-
198-
$locale = null;
199-
200-
if ($locale) {
201-
// filter non-matching locales, note that we could do this with the QueryBuilder
202-
// but currently searching array values is not supported by jackalope-doctrine-dbal.
203-
array_filter($res, function ($route) use ($locale) {
204-
if ($route->getDefault('_locale') != $locale) {
205-
return false;
206-
}
207-
208-
return true;
209-
});
210-
}
211-
212-
if (count($autoRoutes) > 1) {
213-
throw new Exception\MoreThanOneAutoRoute($document);
214-
} elseif (count($autoRoutes) == 1) {
215-
$autoRoute = $autoRoutes->first();
216-
} else {
217-
$autoRoute = new AutoRoute;
218-
$autoRoute->setRouteContent($document);
219-
}
220-
221-
return $autoRoute;
222-
}
223-
224-
/**
225-
* Fetch all the automatic routes for the given document
226-
*
227-
* @param object $document Mapped document
228-
*
229-
* @return array
230-
*/
231-
public function fetchAutoRoutesForDocument($document)
232-
{
233-
$routes = $this->dm->getReferrers($document, null, 'routeContent');
234-
$routes = $routes->filter(function ($route) {
235-
if ($route instanceof AutoRoute) {
236-
return true;
237-
}
238-
239-
return false;
240-
});
241-
242-
return $routes;
243-
}
244-
245-
public function getDefaultPath()
246-
{
247-
return $this->defaultPath;
248-
}
249-
250-
protected function isDocumentPersisted($document)
251-
{
252-
$metadata = $this->dm->getClassMetadata(get_class($document));
253-
$id = $metadata->getIdentifierValue($document);
254-
255-
return $this->phpcrSession->nodeExists($id);
70+
return $this->bucf->hasMapping(ClassUtils::getClass($document));
25671
}
25772
}

AutoRoute/Builder.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoRouteBundle\AutoRoute;
4+
5+
use PHPCR\SessionInterface as PhpcrSession;
6+
7+
/**
8+
* This class uses the actions defined builder units construct
9+
* a path.
10+
*
11+
* @author Daniel Leech <[email protected]>
12+
*/
13+
class Builder implements BuilderInterface
14+
{
15+
protected $phpcrSession;
16+
17+
public function __construct(PhpcrSession $phpcrSession)
18+
{
19+
$this->phpcrSession = $phpcrSession;
20+
}
21+
22+
public function build(BuilderUnitInterface $builderUnit, BuilderContext $context)
23+
{
24+
$builderUnit->pathAction($context);
25+
26+
$exists = $this->phpcrSession->nodeExists($context->getPath());
27+
28+
if ($exists) {
29+
// todo: check to see if the existing node references
30+
// this content... i.e. allow updates to pass.
31+
32+
$builderUnit->existsAction($context);
33+
} else {
34+
$builderUnit->notExistsAction($context);
35+
}
36+
}
37+
}

AutoRoute/BuilderContext.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoRouteBundle\AutoRoute;
4+
5+
use Symfony\Cmf\Bundle\RoutingExtraBundle\Document\Route;
6+
7+
/**
8+
* @author Daniel Leech <[email protected]>
9+
*/
10+
class BuilderContext
11+
{
12+
protected $pathStack = array();
13+
protected $routeStack = array();
14+
15+
protected $isLastBuilder = false;
16+
17+
public function addPath($part)
18+
{
19+
$this->pathStack[] = $part;
20+
}
21+
22+
public function getLastPath()
23+
{
24+
return end($this->pathStack);
25+
}
26+
27+
public function replaceLastPath($path)
28+
{
29+
array_pop($this->pathStack);
30+
$this->pathStack[] = $path;
31+
}
32+
33+
public function getPathStack()
34+
{
35+
return $this->pathStack;
36+
}
37+
38+
public function addRoute($route)
39+
{
40+
$this->routeStack[]= $route;
41+
}
42+
43+
public function getRouteStack()
44+
{
45+
return $this->routeStack;
46+
}
47+
48+
public function getLastRoute()
49+
{
50+
return end($this->routeStack);
51+
}
52+
53+
public function getPath()
54+
{
55+
return implode('/', $this->pathStack);
56+
}
57+
58+
public function isLastBuilder($isLastBuilder = null)
59+
{
60+
if (null === $isLastBuilder) {
61+
return $this->isLastBuilder;
62+
}
63+
64+
$this->isLastBuilder = $isLastBuilder;
65+
}
66+
67+
public function setObject($object)
68+
{
69+
$this->object = $object;
70+
}
71+
72+
public function getObject()
73+
{
74+
return $this->object;
75+
}
76+
}

AutoRoute/BuilderInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoRouteBundle\AutoRoute;
4+
5+
/**
6+
* @author Daniel Leech <[email protected]>
7+
*/
8+
interface BuilderInterface
9+
{
10+
public function build(BuilderUnitInterface $builderUnit, BuilderContext $context);
11+
}
12+

0 commit comments

Comments
 (0)