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

Commit 77b7f69

Browse files
committed
Merge pull request #4 from symfony-cmf/dev
Refactoring
2 parents bd16bad + 1ee31f0 commit 77b7f69

Some content is hidden

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

59 files changed

+1686
-1047
lines changed

AutoRoute/AutoRouteMaker.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
4+
5+
use Doctrine\ODM\PHPCR\DocumentManager;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Document\AutoRoute;
7+
8+
/**
9+
* @author Daniel Leech <[email protected]>
10+
*/
11+
class AutoRouteMaker
12+
{
13+
protected $dm;
14+
15+
public function __construct(DocumentManager $dm)
16+
{
17+
$this->dm = $dm;
18+
}
19+
20+
public function createOrUpdateAutoRoute(AutoRouteStack $autoRouteStack)
21+
{
22+
$context = $autoRouteStack->getContext();
23+
$content = $context->getContent();
24+
25+
$autoRoute = $this->getAutoRouteForDocument($content);
26+
27+
if (null === $autoRoute) {
28+
$autoRoute = new AutoRoute;
29+
$autoRoute->setParent($context->getTopRoute());
30+
$autoRoute->setRouteContent($content);
31+
}
32+
33+
$autoRoute->setName($autoRouteStack->getPath());
34+
35+
$autoRouteStack->addRoute($autoRoute);
36+
}
37+
38+
protected function getAutoRouteForDocument($document)
39+
{
40+
if (!$this->documentIsPersisted($document)) {
41+
return null;
42+
}
43+
44+
$dm = $this->dm;
45+
$uow = $dm->getUnitOfWork();
46+
47+
$referrers = $this->dm->getReferrers($document);
48+
49+
if ($referrers->count() == 0) {
50+
return null;
51+
}
52+
53+
// Filter non auto-routes
54+
$referrers = $referrers->filter(function ($referrer) {
55+
if ($referrer instanceof AutoRoute) {
56+
return true;
57+
}
58+
59+
return false;
60+
});
61+
62+
$metadata = $dm->getClassMetadata(get_class($document));
63+
64+
$locale = null; // $uow->getLocale($document, $locale);
65+
66+
// If the document is translated, filter locales
67+
if (null !== $locale) {
68+
throw new \Exception(
69+
'Translations not yet supported for Auto Routes - '.
70+
'Should be easy.'
71+
);
72+
73+
// array_filter($referrers, function ($referrer) use ($dm, $uow, $locale) {
74+
// $metadata = $dm->getClassMetadata($refferer);
75+
// if ($locale == $uow->getLocaleFor($referrer, $referrer)) {
76+
// return true;
77+
// }
78+
79+
// return false;
80+
// });
81+
}
82+
83+
if ($referrers->count() > 1) {
84+
throw new \RuntimeException(sprintf(
85+
'More than one auto route for document "%s"',
86+
get_class($document)
87+
));
88+
}
89+
90+
return $referrers->current();
91+
}
92+
93+
protected function documentIsPersisted($document)
94+
{
95+
$metadata = $this->dm->getClassMetadata(get_class($document));
96+
$id = $metadata->getIdentifierValue($document);
97+
$phpcrSession = $this->dm->getPhpcrSession();
98+
return $phpcrSession->nodeExists($id);
99+
}
100+
}

AutoRoute/AutoRouteManager.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
44

55
use Doctrine\ODM\PHPCR\DocumentManager;
6-
use Metadata\MetadataFactoryInterface;
7-
use Symfony\Cmf\Bundle\RoutingAutoBundle\Document\AutoRoute;
8-
use Symfony\Cmf\Bundle\CoreBundle\Slugifier\SlugifierInterface;
9-
use PHPCR\Util\NodeHelper;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\AutoRouteStack;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack\Builder;
108
use Doctrine\Common\Util\ClassUtils;
119

1210
/**
@@ -16,11 +14,12 @@
1614
*/
1715
class AutoRouteManager
1816
{
19-
protected $bucf;
17+
protected $factory;
2018

21-
public function __construct(BuilderUnitChainFactory $bucf)
19+
public function __construct(Factory $factory, Builder $builder)
2220
{
23-
$this->bucf = $bucf;
21+
$this->factory = $factory;
22+
$this->builder = $builder;
2423
}
2524

2625
/**
@@ -31,16 +30,24 @@ public function __construct(BuilderUnitChainFactory $bucf)
3130
*
3231
* @param object Mapped document for which to generate the AutoRoute
3332
*
34-
* @return AutoRoute
33+
* @return BuilderContext
3534
*/
3635
public function updateAutoRouteForDocument($document)
3736
{
37+
$classFqn = ClassUtils::getClass($document);
38+
3839
$context = new BuilderContext;
39-
$context->setObject($document);
40+
$context->setContent($document);
4041

41-
$builderUnitChain = $this->bucf->getChain(ClassUtils::getClass($document));
42+
// build chain
43+
$builderUnitChain = $this->factory->getRouteStackBuilderUnitChain($classFqn);
4244
$builderUnitChain->executeChain($context);
4345

46+
// persist the auto route
47+
$autoRouteStack = new AutoRouteStack($context);
48+
$builderUnit = $this->factory->getContentNameBuilderUnit($classFqn);
49+
$this->builder->build($autoRouteStack, $builderUnit);
50+
4451
return $context;
4552
}
4653

@@ -67,6 +74,6 @@ public function removeAutoRoutesForDocument($document)
6774
*/
6875
public function isAutoRouteable($document)
6976
{
70-
return $this->bucf->hasMapping(ClassUtils::getClass($document));
77+
return $this->factory->hasMapping(ClassUtils::getClass($document));
7178
}
7279
}

AutoRoute/AutoRouteStack.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Document\AutoRoute;
6+
7+
/**
8+
* Special sub class for AutoRoutes.
9+
*
10+
* This enables us to reuse the same strategies for the
11+
* creation of auto routes as for the creation of the content
12+
* path routes.
13+
*
14+
* @author Daniel Leech <[email protected]>
15+
*/
16+
class AutoRouteStack extends RouteStack
17+
{
18+
public function close()
19+
{
20+
if (count($this->routes) > 1) {
21+
throw new \RuntimeException('You can only add one route to the AutoRouteStack.');
22+
}
23+
24+
parent::close();
25+
}
26+
27+
public function addRoute($route)
28+
{
29+
parent::addRoute($route);
30+
}
31+
}

AutoRoute/Builder.php

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

AutoRoute/BuilderContext.php

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,68 +9,75 @@
99
*/
1010
class BuilderContext
1111
{
12-
protected $pathStack = array();
13-
protected $routeStack = array();
12+
protected $routeStacks = array();
13+
protected $stagedRouteStack;
14+
protected $content;
1415

15-
protected $isLastBuilder = false;
16-
17-
public function addPath($part)
16+
public function getRoutes()
1817
{
19-
$this->pathStack[] = $part;
20-
}
18+
$routes = array();
19+
foreach ($this->routeStacks as $routeStack) {
20+
$routes = array_merge($routes, $routeStack->getRoutes());
21+
}
2122

22-
public function getLastPath()
23-
{
24-
return end($this->pathStack);
23+
return $routes;
2524
}
2625

27-
public function replaceLastPath($path)
26+
public function stageRouteStack(RouteStack $routeStack)
2827
{
29-
array_pop($this->pathStack);
30-
$this->pathStack[] = $path;
28+
$this->stagedRouteStack = $routeStack;
3129
}
3230

33-
public function getPathStack()
31+
public function commitRouteStack()
3432
{
35-
return $this->pathStack;
36-
}
33+
if (null === $this->stagedRouteStack) {
34+
throw new \RuntimeException(
35+
'Cannot commit route stack when there is no route stack to commit '.
36+
'(use stageRouteStack to stage)'
37+
);
38+
}
3739

38-
public function addRoute($route)
39-
{
40-
$this->routeStack[]= $route;
41-
}
40+
if (false === $this->stagedRouteStack->isClosed()) {
41+
throw new \RuntimeException(
42+
'Staged route stack is not closed, cannot commit.'
43+
);
44+
}
4245

43-
public function getRouteStack()
44-
{
45-
return $this->routeStack;
46+
$this->routeStacks[] = $this->stagedRouteStack;
47+
$this->stagedRouteStack = null;
4648
}
4749

48-
public function getLastRoute()
50+
public function getRouteStacks()
4951
{
50-
return end($this->routeStack);
52+
return $this->routeStacks;
5153
}
5254

53-
public function getPath()
55+
public function getTopRoute()
5456
{
55-
return implode('/', $this->pathStack);
57+
$routes = $this->getRoutes();
58+
return end($routes);
5659
}
5760

58-
public function isLastBuilder($isLastBuilder = null)
61+
public function getFullPath()
5962
{
60-
if (null === $isLastBuilder) {
61-
return $this->isLastBuilder;
63+
$paths = array();
64+
foreach ($this->routeStacks as $routeStack) {
65+
$paths[] = $routeStack->getPath();
6266
}
6367

64-
$this->isLastBuilder = $isLastBuilder;
68+
$path = implode('/', $paths);
69+
70+
return $path;
71+
6572
}
6673

67-
public function setObject($object)
74+
public function setContent($content)
6875
{
69-
$this->object = $object;
76+
$this->content = $content;
7077
}
7178

72-
public function getObject()
79+
public function getContent()
7380
{
74-
return $this->object;
81+
return $this->content;
7582
}
7683
}

AutoRoute/BuilderInterface.php

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

AutoRoute/BuilderUnit.php

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

0 commit comments

Comments
 (0)