Skip to content

Commit 1f79119

Browse files
committed
Merge pull request #112 from symfony-cmf/create-models
move document to model
2 parents f3dda5c + f576865 commit 1f79119

Some content is hidden

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

45 files changed

+1383
-872
lines changed

Admin/RouteAdmin.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
1010
use Symfony\Component\HttpFoundation\Request;
1111
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
12+
use PHPCR\Util\PathHelper;
1213

1314
class RouteAdmin extends Admin
1415
{
@@ -74,7 +75,8 @@ protected function configureDatagridFilters(DatagridMapper $datagridMapper)
7475

7576
public function setRouteRoot($routeRoot)
7677
{
77-
$this->routeRoot = $routeRoot;
78+
// TODO: fix widget to show root node when root is selectable
79+
$this->routeRoot = PathHelper::getParentPath($routeRoot);
7880
}
7981

8082
public function setContentRoot($contentRoot)

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
Changelog
22
=========
33

4+
* **2013-07-19**: [Model] Separated database agnostic, doctrine generic and
5+
PHPCR-ODM specific code to prepare for Doctrine ORM support.
46
* **2013-07-17**: [FormType] Moved TermsFormType to CoreBundle and renamed it to CheckboxUrlLableFormType
57

68
1.1.0-beta2
7-
-----------
89

910
* **2013-05-28**: [Bundle] Only include Doctrine PHPCR compiler pass if PHPCR-ODM is present
1011
* **2013-05-25**: [Bundle] Drop symfony_ from symfony_cmf prefix

CmfRoutingBundle.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public function build(ContainerBuilder $container)
2727

2828
if (class_exists('Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass')) {
2929
$container->addCompilerPass($this->buildBasePhpcrCompilerPass());
30+
$container->addCompilerPass(
31+
DoctrinePhpcrMappingsPass::createXmlMappingDriver(
32+
array(
33+
realpath(__DIR__ . '/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\RoutingBundle\Model',
34+
realpath(__DIR__ . '/Resources/config/doctrine-phpcr') => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr',
35+
),
36+
array('cmf_routing.manager_name')
37+
)
38+
);
3039
}
3140
}
3241

@@ -47,7 +56,7 @@ private function buildBasePhpcrCompilerPass()
4756
$driver,
4857
array('Symfony\Component\Routing'),
4958
array('cmf_routing.manager_name'),
50-
false // TODO: once we have config for orm or phpcr-odm, configure the enabled parameter
59+
'cmf_routing.backend_type_phpcr'
5160
);
5261
}
5362
}

DependencyInjection/CmfRoutingExtension.php

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ public function load(array $configs, ContainerBuilder $container)
4343
}
4444

4545
$this->setupFormTypes($config, $container, $loader);
46-
47-
if ($config['use_sonata_admin']) {
48-
$this->loadSonataAdmin($config, $loader, $container);
49-
}
5046
}
5147

5248
public function setupFormTypes(array $config, ContainerBuilder $container, LoaderInterface $loader)
@@ -87,22 +83,18 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container,
8783
$container->setParameter($this->getAlias() . '.defined_templates_class', $controllerForTemplates);
8884
$container->setParameter($this->getAlias() . '.uri_filter_regexp', $config['uri_filter_regexp']);
8985

90-
$loader->load('cmf_routing.xml');
91-
$container->setParameter($this->getAlias() . '.routing_repositoryroot', $config['routing_repositoryroot']);
92-
if (isset($config['locales']) && $config['locales']) {
93-
$container->setParameter($this->getAlias() . '.locales', $config['locales']);
94-
} else {
95-
$container->removeDefinition('cmf_routing.phpcrodm_route_localeupdater_listener');
96-
}
86+
$loader->load('dynamic_routing.xml');
9787

98-
$container->setAlias('cmf_routing.route_provider', $config['route_provider_service_id']);
99-
$container->setAlias('cmf_routing.content_repository', $config['content_repository_service_id']);
88+
if (isset($config['phpcr_provider'])) {
89+
$this->loadPhpcrProvider($config['phpcr_provider'], $loader, $container);
90+
}
10091

101-
$routeProvider = $container->getDefinition($this->getAlias() . '.default_route_provider');
102-
$routeProvider->replaceArgument(0, new Reference($config['manager_registry']));
103-
$contentRepository = $container->getDefinition($this->getAlias() . '.default_content_repository');
104-
$contentRepository->replaceArgument(0, new Reference($config['manager_registry']));
105-
$container->setParameter($this->getAlias() . '.manager_name', $config['manager_name']);
92+
if (isset($config['route_provider_service_id'])) {
93+
$container->setAlias('cmf_routing.route_provider', $config['route_provider_service_id']);
94+
}
95+
if (isset($config['content_repository_service_id'])) {
96+
$container->setAlias('cmf_routing.content_repository', $config['content_repository_service_id']);
97+
}
10698

10799
$dynamic = $container->getDefinition($this->getAlias().'.dynamic_router');
108100

@@ -128,16 +120,43 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container,
128120
}
129121
}
130122

131-
public function loadSonataAdmin($config, XmlFileLoader $loader, ContainerBuilder $container)
123+
public function loadPhpcrProvider($config, XmlFileLoader $loader, ContainerBuilder $container)
124+
{
125+
$loader->load('provider_phpcr.xml');
126+
127+
$container->setParameter($this->getAlias() . '.backend_type_phpcr', true);
128+
129+
$container->setParameter($this->getAlias() . '.phpcr_provider.route_basepath', $config['route_basepath']);
130+
$container->setParameter($this->getAlias() . '.phpcr_provider.content_basepath', $config['content_basepath']);
131+
132+
$routeProvider = $container->getDefinition($this->getAlias() . '.phpcr_route_provider');
133+
$routeProvider->replaceArgument(0, new Reference($config['manager_registry']));
134+
$contentRepository = $container->getDefinition($this->getAlias() . '.phpcr_content_repository');
135+
$contentRepository->replaceArgument(0, new Reference($config['manager_registry']));
136+
$container->setParameter($this->getAlias() . '.manager_name', $config['manager_name']);
137+
138+
$container->setAlias($this->getAlias() . '.route_provider', $this->getAlias() . '.phpcr_route_provider');
139+
$container->setAlias($this->getAlias() . '.content_repository', $this->getAlias() . '.phpcr_content_repository');
140+
141+
if (isset($config['locales']) && $config['locales']) {
142+
$container->setParameter($this->getAlias() . '.locales', $config['locales']);
143+
} else {
144+
$container->removeDefinition('cmf_routing.phpcrodm_route_locale_listener');
145+
}
146+
147+
if ($config['use_sonata_admin']) {
148+
$this->loadSonataPhpcrAdmin($config, $loader, $container);
149+
}
150+
}
151+
152+
public function loadSonataPhpcrAdmin($config, XmlFileLoader $loader, ContainerBuilder $container)
132153
{
133154
$bundles = $container->getParameter('kernel.bundles');
134155
if ('auto' === $config['use_sonata_admin'] && !isset($bundles['SonataDoctrinePHPCRAdminBundle'])) {
135156
return;
136157
}
137158

138-
$loader->load('routing-admin.xml');
139-
$container->setParameter($this->getAlias() . '.content_basepath', $config['content_basepath']);
140-
$container->setParameter($this->getAlias() . '.route_basepath', $config['route_basepath']);
159+
$loader->load('admin_phpcr.xml');
141160
}
142161

143162
/**

DependencyInjection/Configuration.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,31 @@ public function getConfigTreeBuilder()
5858
->useAttributeAsKey('alias')
5959
->prototype('scalar')->end()
6060
->end()
61-
->scalarNode('manager_registry')->defaultValue('doctrine_phpcr')->end()
62-
->scalarNode('manager_name')->defaultValue('default')->end()
61+
->arrayNode('phpcr_provider')
62+
->children()
63+
->scalarNode('manager_registry')->defaultValue('doctrine_phpcr')->end()
64+
->scalarNode('manager_name')->defaultNull()->end()
65+
->scalarNode('route_basepath')->defaultValue('/cms/routes')->end()
66+
->scalarNode('content_basepath')->defaultValue('/cms/content')->end()
67+
->enumNode('use_sonata_admin')
68+
->values(array(true, false, 'auto'))
69+
->defaultValue('auto')
70+
->end()
71+
->end()
72+
->end()
6373
->scalarNode('uri_filter_regexp')->defaultValue('')->end()
64-
->scalarNode('route_provider_service_id')->defaultValue('cmf_routing.default_route_provider')->end()
74+
->scalarNode('route_provider_service_id')->end()
6575
->arrayNode('route_filters_by_id')
6676
->canBeUnset()
6777
->useAttributeAsKey('id')
6878
->prototype('scalar')->end()
6979
->end()
70-
->scalarNode('content_repository_service_id')->defaultValue('cmf_routing.default_content_repository')->end()
71-
->scalarNode('routing_repositoryroot')->defaultValue('/cms/routes')->end()
80+
->scalarNode('content_repository_service_id')->end()
7281
->arrayNode('locales')
7382
->prototype('scalar')->end()
7483
->end()
7584
->end()
7685
->end()
77-
->enumNode('use_sonata_admin')
78-
->values(array(true, false, 'auto'))
79-
->defaultValue('auto')
80-
->end()
81-
->scalarNode('content_basepath')->defaultValue('/cms/content')->end()
82-
// TODO: fix widget to show root node when root is selectable, then use routing_repositoryroot for both
83-
->scalarNode('route_basepath')->defaultValue('/cms')->end()
8486
->end()
8587
;
8688

Doctrine/DoctrineProvider.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine;
4+
5+
use Doctrine\Common\Persistence\ManagerRegistry;
6+
use Doctrine\Common\Persistence\ObjectManager;
7+
8+
/**
9+
* Abstract class for doctrine based content repository and route provider
10+
* implementations.
11+
*
12+
* @author Uwe Jäger
13+
* @author David Buchmann <[email protected]>
14+
*/
15+
abstract class DoctrineProvider
16+
{
17+
/**
18+
* If this is null, the manager registry will return the default manager.
19+
*
20+
* @var string|null Name of object manager to use
21+
*/
22+
protected $managerName;
23+
24+
/**
25+
* @var ManagerRegistry
26+
*/
27+
protected $managerRegistry;
28+
29+
/**
30+
* Class name of the object class to find, null for PHPCR-ODM as it can
31+
* determine the class on its own.
32+
*
33+
* @var string|null
34+
*/
35+
protected $className;
36+
37+
/**
38+
* @param ManagerRegistry $managerRegistry
39+
*/
40+
public function __construct(ManagerRegistry $managerRegistry, $className = null)
41+
{
42+
$this->managerRegistry = $managerRegistry;
43+
}
44+
45+
/**
46+
* Set the object manager name to use for this loader. If not set, the
47+
* default manager as decided by the manager registry will be used.
48+
*
49+
* @param string|null $managerName
50+
*/
51+
public function setManagerName($managerName)
52+
{
53+
$this->managerName = $managerName;
54+
}
55+
56+
/**
57+
* Get the object manager named $managerName from the registry.
58+
*
59+
* @return ObjectManager
60+
*/
61+
protected function getObjectManager()
62+
{
63+
return $this->managerRegistry->getManager($this->managerName);
64+
}
65+
}

Doctrine/Phpcr/ContentRepository.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr;
4+
5+
use Symfony\Cmf\Component\Routing\ContentRepositoryInterface;
6+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\DoctrineProvider;
7+
8+
/**
9+
* Implement ContentRepositoryInterface for PHPCR-ODM
10+
*
11+
* This is <strong>NOT</strong> not a doctrine repository but just the content
12+
* provider for the NestedMatcher. (you could of course implement this
13+
* interface in a repository class, if you need that)
14+
*
15+
* @author Uwe Jäger
16+
*/
17+
class ContentRepository extends DoctrineProvider implements ContentRepositoryInterface
18+
{
19+
/**
20+
* {@inheritDoc}
21+
*/
22+
public function findById($id)
23+
{
24+
return $this->getObjectManager()->find(null, $id);
25+
}
26+
27+
/**
28+
* {@inheritDoc}
29+
*/
30+
public function getContentId($content)
31+
{
32+
if (! is_object($content)) {
33+
return null;
34+
}
35+
try {
36+
return $this->getObjectManager()->getUnitOfWork()->getDocumentId($content);
37+
} catch (\Exception $e) {
38+
return null;
39+
}
40+
}
41+
}

Listener/IdPrefix.php renamed to Doctrine/Phpcr/IdPrefixListener.php

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

3-
namespace Symfony\Cmf\Bundle\RoutingBundle\Listener;
3+
namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr;
44

5-
use Symfony\Cmf\Bundle\RoutingBundle\Document\Route;
5+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
66
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
77

88
/**
9-
* Doctrine PHPCR-ODM listener to set the idPrefix on new routes
9+
* Doctrine PHPCR-ODM listener to set the idPrefix on routes
1010
*
11-
* @author david.buchmann@liip.ch
11+
* @author David Buchmann <mail@davidbu.ch>
1212
*/
13-
class IdPrefix
13+
class IdPrefixListener
1414
{
1515
/**
1616
* The prefix to add to the url to create the repository path
@@ -48,7 +48,7 @@ protected function updateId(LifecycleEventArgs $args)
4848

4949
// only update route objects and only if the prefix can match, to allow
5050
// for more than one listener and more than one route root
51-
if ($doc instanceof Route
51+
if (($doc instanceof PrefixInterface)
5252
&& ! strncmp($this->idPrefix, $doc->getId(), strlen($this->idPrefix))
5353
) {
5454
$doc->setPrefix($this->idPrefix);

Listener/LocaleUpdater.php renamed to Doctrine/Phpcr/LocaleListener.php

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

3-
namespace Symfony\Cmf\Bundle\RoutingBundle\Listener;
3+
namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr;
44

5-
use Symfony\Cmf\Bundle\RoutingBundle\Document\Route;
65
use Doctrine\ODM\PHPCR\Event\MoveEventArgs;
76
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
87

8+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
9+
910
/**
1011
* Doctrine PHPCR-ODM listener to update the locale on routes based on the URL.
1112
*
1213
* It uses the idPrefix and looks at the path segment right after the prefix to
1314
* determine if that segment matches one of the configured locales and if so
1415
* sets a requirement and default named _locale for that locale.
1516
*
16-
* @author David Buchmann <david@liip.ch>
17+
* @author David Buchmann <mail@davidbu.ch>
1718
*/
18-
class LocaleUpdater
19+
class LocaleListener
1920
{
2021
/**
2122
* The prefix to add to the url to create the repository path

Doctrine/Phpcr/PrefixInterface.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr;
4+
5+
/**
6+
* An interface for PHPCR route documents.
7+
*
8+
* We use the repository path as static part of the URL, but the documents
9+
* need to know what their base path is to remove that from the URL.
10+
*/
11+
interface PrefixInterface
12+
{
13+
/**
14+
* @return string the full path of this document in the repository.
15+
*/
16+
public function getId();
17+
18+
/**
19+
* @param string $prefix The path in the repository to the routing root
20+
* document.
21+
*/
22+
public function setPrefix($prefix);
23+
}

0 commit comments

Comments
 (0)