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

Commit 3030eea

Browse files
committed
Merge pull request #11 from dantleech/route_maker
Refactored route patcher / makers
2 parents 1d82866 + 288c0f5 commit 3030eea

23 files changed

+356
-189
lines changed

AutoRoute/Factory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Factory
2222
'provider' => array(),
2323
'exists_action' => array(),
2424
'not_exists_action' => array(),
25+
'route_maker' => array(),
2526
);
2627

2728
protected $container;

AutoRoute/PathExists/AutoIncrementPath.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\BuilderContext;
77
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
88
use Doctrine\ODM\PHPCR\DocumentManager;
9-
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMaker;
9+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMakerInterface;
1010

1111
/**
1212
* @author Daniel Leech <[email protected]>
@@ -16,7 +16,7 @@ class AutoIncrementPath implements PathActionInterface
1616
protected $dm;
1717
protected $routeMaker;
1818

19-
public function __construct(DocumentManager $dm, RouteMaker $routeMaker)
19+
public function __construct(DocumentManager $dm, RouteMakerInterface $routeMaker)
2020
{
2121
$this->dm = $dm;
2222
$this->routeMaker = $routeMaker;
@@ -38,6 +38,6 @@ public function execute(RouteStack $routeStack)
3838

3939
$routeStack->replaceLastPathElement(basename($newPath));
4040

41-
$this->routeMaker->makeRoutes($routeStack);
41+
$this->routeMaker->make($routeStack);
4242
}
4343
}

AutoRoute/PathNotExists/CreatePath.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\PathActionInterface;
66
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
7-
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMaker;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMakerInterface;
88

99
/**
1010
* @author Daniel Leech <[email protected]>
@@ -13,7 +13,7 @@ class CreatePath implements PathActionInterface
1313
{
1414
protected $routeMaker;
1515

16-
public function __construct(RouteMaker $routeMaker)
16+
public function __construct(RouteMakerInterface $routeMaker)
1717
{
1818
$this->routeMaker = $routeMaker;
1919
}
@@ -24,6 +24,6 @@ public function init(array $options)
2424

2525
public function execute(RouteStack $routeStack)
2626
{
27-
$this->routeMaker->makeRoutes($routeStack);
27+
$this->routeMaker->make($routeStack);
2828
}
2929
}

AutoRoute/RouteMaker.php

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

AutoRoute/AutoRouteMaker.php renamed to AutoRoute/RouteMaker/AutoRouteMaker.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
<?php
22

3-
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMaker;
44

55
use Doctrine\ODM\PHPCR\DocumentManager;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMakerInterface;
67
use Symfony\Cmf\Bundle\RoutingAutoBundle\Document\AutoRoute;
8+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
79

810
/**
911
* This class is responsible for creating and updating the actual
1012
* AutoRoute documents.
1113
*
1214
* @author Daniel Leech <[email protected]>
1315
*/
14-
class AutoRouteMaker
16+
class AutoRouteMaker implements RouteMakerInterface
1517
{
1618
protected $dm;
1719

@@ -20,9 +22,9 @@ public function __construct(DocumentManager $dm)
2022
$this->dm = $dm;
2123
}
2224

23-
public function createOrUpdateAutoRoute(AutoRouteStack $autoRouteStack)
25+
public function make(RouteStack $routeStack)
2426
{
25-
$context = $autoRouteStack->getContext();
27+
$context = $routeStack->getContext();
2628
$content = $context->getContent();
2729

2830
$autoRoute = $this->getAutoRouteForDocument($content);
@@ -33,9 +35,9 @@ public function createOrUpdateAutoRoute(AutoRouteStack $autoRouteStack)
3335
$autoRoute->setRouteContent($content);
3436
}
3537

36-
$autoRoute->setName($autoRouteStack->getPath());
38+
$autoRoute->setName($routeStack->getPath());
3739

38-
$autoRouteStack->addRoute($autoRoute);
40+
$routeStack->addRoute($autoRoute);
3941
}
4042

4143
protected function getAutoRouteForDocument($document)

AutoRoute/RouteMaker/DefaultMaker.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMaker;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMakerInterface;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\AutoRouteStack;
8+
9+
/**
10+
* Default route maker class - automatically delegates to an
11+
* appropriate maker depending on the context.
12+
*
13+
* @author Daniel Leech <[email protected]>
14+
*/
15+
class DefaultMaker implements RouteMakerInterface
16+
{
17+
protected $autoRouteMaker;
18+
protected $patcher;
19+
20+
public function __construct(RouteMakerInterface $autoRouteMaker, RouteMakerInterface $patcher)
21+
{
22+
$this->autoRouteMaker = $autoRouteMaker;
23+
$this->patcher = $patcher;
24+
}
25+
26+
public function make(RouteStack $routeStack)
27+
{
28+
if ($routeStack instanceOf AutoRouteStack) {
29+
$this->autoRouteMaker->make($routeStack);
30+
} else {
31+
$this->patcher->make($routeStack);
32+
}
33+
}
34+
}

AutoRoute/RoutePatcher/GenericPatcher.php renamed to AutoRoute/RouteMaker/GenericMaker.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RoutePatcher;
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMaker;
44

5-
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RoutePatcherInterface;
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMakerInterface;
66
use Doctrine\ODM\PHPCR\DocumentManager;
77
use Doctrine\ODM\PHPCR\Document\Generic;
88
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
@@ -13,24 +13,24 @@
1313
*
1414
* @author Daniel Leech <[email protected]>
1515
*/
16-
class GenericPatcher implements RoutePatcherInterface
16+
class GenericMaker implements RouteMakerInterface
1717
{
1818
public function __construct(DocumentManager $dm)
1919
{
2020
$this->dm = $dm;
2121
}
2222

23-
public function patch(RouteStack $routeStack)
23+
public function make(RouteStack $routeStack)
2424
{
2525
$paths = $routeStack->getFullPaths();
26+
$meta = $this->dm->getClassMetadata('Doctrine\ODM\PHPCR\Document\Generic');
2627

2728
foreach ($paths as $path) {
2829
$absPath = '/'.$path;
2930
$doc = $this->dm->find(null, $absPath);
3031

3132
if (null === $doc) {
3233
$doc = new Generic;
33-
$meta = $this->dm->getClassMetadata(get_class($doc));
3434
$meta->setIdentifierValue($doc, $absPath);
3535
}
3636

AutoRoute/RouteMaker/RouteMaker.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMaker;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteMakerInterface;
6+
use Doctrine\ODM\PHPCR\DocumentManager;
7+
use Doctrine\ODM\PHPCR\Document\Generic;
8+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
9+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Exception\MissingOptionException;
10+
use Symfony\Cmf\Bundle\RoutingExtraBundle\Document\Route;
11+
12+
/**
13+
* This class will make the Route classes using
14+
* Generic documents using.
15+
*
16+
* @author Daniel Leech <[email protected]>
17+
*/
18+
class RouteMaker implements RouteMakerInterface
19+
{
20+
protected $defaults = array();
21+
22+
public function __construct(DocumentManager $dm)
23+
{
24+
$this->dm = $dm;
25+
}
26+
27+
public function init(array $options)
28+
{
29+
$options = array_merge(array(
30+
'defaults' => array(),
31+
), $options);
32+
33+
$this->defaults = $options['defaults'];
34+
}
35+
36+
public function make(RouteStack $routeStack)
37+
{
38+
$paths = $routeStack->getFullPaths();
39+
40+
foreach ($paths as $path) {
41+
$absPath = '/'.$path;
42+
$doc = $this->dm->find(null, $absPath);
43+
44+
if (null === $doc) {
45+
$doc = new Route;
46+
$doc->setDefaults($this->defaults);
47+
$doc->setId($absPath);
48+
}
49+
50+
$routeStack->addRoute($doc);
51+
}
52+
}
53+
}
54+

AutoRoute/RouteMakerInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack;
6+
7+
/**
8+
* Classes implementing this interface complete a route stack
9+
* by adding one PHPCR-ODM document for each path element contained
10+
* in the RouteStack.
11+
*
12+
* @author Daniel Leech <[email protected]>
13+
* @date 13/03/24
14+
*/
15+
interface RouteMakerInterface
16+
{
17+
public function make(RouteStack $routeStack);
18+
}

DependencyInjection/Compiler/AutoRoutePass.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ public function process(ContainerBuilder $container)
2626

2727

2828
$types = array(
29-
'provider', 'exists_action', 'not_exists_action'
29+
'provider',
30+
'exists_action',
31+
'not_exists_action',
32+
'route_maker'
3033
);
3134

3235
foreach ($types as $type) {
@@ -47,5 +50,3 @@ public function process(ContainerBuilder $container)
4750
}
4851
}
4952
}
50-
51-

0 commit comments

Comments
 (0)