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

Commit 04d8a80

Browse files
committed
Merge pull request #446 from symfony-cmf/initializers
adjust initializer documentation to refactoring
2 parents c2d5136 + f990f80 commit 04d8a80

File tree

3 files changed

+182
-62
lines changed

3 files changed

+182
-62
lines changed

bundles/phpcr_odm.rst

Lines changed: 120 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ If you want control over the last modified timestamp, disable
164164
of the PHPCR-ODM documentation.
165165

166166
.. versionadded:: 1.1
167-
168167
Since DoctrinePhpcrBundle 1.1, backend configuration flags are configured
169168
in the ``parameters`` section. They are passed as-is to Jackalope. See the
170169
``RepositoryFactory`` for some more documentation on the meaning of those
@@ -257,7 +256,7 @@ supported by Doctrine.
257256

258257
You can specify the connection name to use if you don't want to use the default
259258
connection. The name must be one of the names of the dbal section in your
260-
Doctrine configuration, see `the Symfony2 Doctrine documentation`_.
259+
Doctrine configuration, see the `Symfony2 Doctrine documentation`_.
261260

262261
You can tune the behaviour with the general Jackalope parameters listed above,
263262
as well as enable transactions (Jackrabbit does not support transactions):
@@ -446,7 +445,7 @@ The session backend configuration looks as follows:
446445
),
447446
));
448447
449-
For more information, please refer to `the official Midgard PHPCR documentation`_.
448+
For more information, please refer to the `official Midgard PHPCR documentation`_.
450449

451450
Profiling and Performance of Jackalope
452451
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1346,13 +1345,18 @@ Repository Initializers
13461345

13471346
The Initializer is the PHPCR equivalent of the ORM schema tools. It
13481347
is used to let bundles register PHPCR node types and to create required base
1349-
paths in the repository. Initializers have to implement
1350-
``Doctrine\Bundle\PHPCRBundle\Initializer``. If you don't need any special
1351-
logic, you can simply configure the ``GenericInitializer`` as service and don't
1352-
need to write any code. The generic initializer expects a name to identify
1353-
the initializer, an array of base paths it will create if they do not exist
1354-
and an optional string defining namespaces and primary / mixin node types in
1355-
the CND language.
1348+
paths in the repository. Initializers have to implement the
1349+
``Doctrine\Bundle\PHPCRBundle\Initializer\InitializerInterface``. If you don't
1350+
need any special logic and want to create plain PHPCR nodes and not documents,
1351+
you can simply define services with ``GenericInitializer``. The generic
1352+
initializer expects a name to identify the initializer, an array of repository
1353+
paths it will create if they do not exist and an optional string defining
1354+
namespaces and primary / mixin node types in the CND language.
1355+
1356+
.. versionadded:: 1.1
1357+
Since version 1.1, the ``GenericInitializer`` expects a name parameter
1358+
as first argument. With 1.0 there is no way to specify a custom name
1359+
for the generic initializer.
13561360

13571361
A service to use the generic initializer looks like this:
13581362

@@ -1361,7 +1365,7 @@ A service to use the generic initializer looks like this:
13611365
.. code-block:: yaml
13621366
13631367
# src/Acme/ContentBundle/Resources/config/services.yml
1364-
acme.phpcr.initializer:
1368+
acme_content.phpcr.initializer:
13651369
class: Doctrine\Bundle\PHPCRBundle\Initializer\GenericInitializer
13661370
arguments:
13671371
- AcmeContentBundle Basepaths
@@ -1373,7 +1377,7 @@ A service to use the generic initializer looks like this:
13731377
.. code-block:: xml
13741378
13751379
<!-- src/Acme/ContentBundle/Resources/config/services.xml -->
1376-
<service id="acme.phpcr.initializer" class="Doctrine\Bundle\PHPCRBundle\Initializer\GenericInitializer">
1380+
<service id="acme_content.phpcr.initializer" class="Doctrine\Bundle\PHPCRBundle\Initializer\GenericInitializer">
13771381
<argument>AcmeContentBundle Basepaths</argument>
13781382
<argument type="collection">
13791383
<argument>%acme.content_basepath%</argument>
@@ -1398,7 +1402,7 @@ A service to use the generic initializer looks like this:
13981402
)
13991403
));
14001404
$definition->addTag('doctrine_phpcr.initializer');
1401-
$container->setDefinition('acme.phpcr.initializer', $definition);
1405+
$container->setDefinition('acme_content.phpcr.initializer', $definition);
14021406
14031407
You can execute your initializers using the following command:
14041408

@@ -1407,9 +1411,110 @@ You can execute your initializers using the following command:
14071411
$ php app/console doctrine:phpcr:repository:init
14081412
14091413
.. versionadded:: 1.1
1414+
Since DoctrinePHPCRBundle 1.1 the load data fixtures command will
1415+
automatically execute the initializers after purging the database.
1416+
1417+
The generic initializer only creates PHPCR nodes. If you want to create
1418+
specific documents, you need your own initializer. The interesting method
1419+
to overwrite is the ``init`` method. It is passed the ``ManagerRegistry``,
1420+
from which you can retrieve the PHPCR session but also the document manager::
1421+
1422+
// src/Acme/BasicCmsBundle/Initializer/SiteInitializer.php
1423+
namespace Acme\ContentBundle\Initializer;
1424+
1425+
use Doctrine\Bundle\PHPCRBundle\Initializer\InitializerInterface;
1426+
use PHPCR\SessionInterface;
1427+
use PHPCR\Util\NodeHelper;
1428+
1429+
class SiteInitializer implements InitializerInterface
1430+
{
1431+
private $basePath;
1432+
1433+
public function __construct($basePath = '/cms')
1434+
{
1435+
$this->basePath = $basePath;
1436+
}
1437+
1438+
public function init(ManagerRegistry $registry)
1439+
{
1440+
$dm = $registry->getManagerForClass('Acme\BasicCmsBundle\Document\Site');
1441+
if ($dm->find(null, $this->basePath)) {
1442+
return;
1443+
}
1444+
1445+
$site = new Acme\BasicCmsBundle\Document\Site();
1446+
$site->setId($this->basePath);
1447+
$dm->persist($site);
1448+
$dm->flush();
1449+
1450+
$session = $registry->getConnection();
1451+
// create the 'cms', 'pages', and 'posts' nodes
1452+
NodeHelper::createPath($session, '/cms/pages');
1453+
NodeHelper::createPath($session, '/cms/posts');
1454+
NodeHelper::createPath($session, '/cms/routes');
1455+
1456+
$session->save();
1457+
}
1458+
1459+
public function getName()
1460+
{
1461+
return 'Site Initializer';
1462+
}
1463+
}
1464+
1465+
.. versionadded:: 1.1
1466+
Since version 1.1, the init method is passed the ``ManagerRegistry`` rather
1467+
than the PHPCR ``SessionInterface`` to allow the creation of documents in
1468+
initializers. With 1.0, you would need to manually set the ``phpcr:class``
1469+
property to the right value.
1470+
1471+
Define a service for your initializer as follows:
14101472

1411-
Since DoctrinePHPCRBundle 1.1 the load data fixtures command will automatically
1412-
execute the initializers after purging the database.
1473+
.. configuration-block::
1474+
1475+
.. code-block:: yaml
1476+
1477+
# src/Acme/BasicCmsBundle/Resources/config/config.yml
1478+
services:
1479+
# ...
1480+
acme_content.phpcr.initializer.site:
1481+
class: Acme\BasicCmsBundle\Initializer\SiteInitializer
1482+
tags:
1483+
- { name: doctrine_phpcr.initializer }
1484+
1485+
.. code-block:: xml
1486+
1487+
<!-- src/Acme/BasicCmsBUndle/Resources/config/config.php
1488+
<?xml version="1.0" encoding="UTF-8" ?>
1489+
<container xmlns="http://symfony.com/schema/dic/services"
1490+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1491+
xmlns:acme_demo="http://www.example.com/symfony/schema/"
1492+
xsi:schemaLocation="http://symfony.com/schema/dic/services
1493+
http://symfony.com/schema/dic/services/services-1.0.xsd">
1494+
1495+
<!-- ... -->
1496+
<services>
1497+
<!-- ... -->
1498+
<service id="acme_content.phpcr.initializer.site"
1499+
class="Acme\BasicCmsBundle\Initializer\SiteInitializer">
1500+
<tag name="doctrine_phpcr.initializer"/>
1501+
</service>
1502+
</services>
1503+
1504+
</container>
1505+
1506+
.. code-block:: php
1507+
1508+
// src/Acme/BasicCmsBundle/Resources/config/config.php
1509+
1510+
// ...
1511+
$container
1512+
->register(
1513+
'acme_content.phpcr.initializer.site',
1514+
'Acme\BasicCmsBundle\Initializer\SiteInitializer'
1515+
)
1516+
->addTag('doctrine_phpcr.initializer', array('name' => 'doctrine_phpcr.initializer')
1517+
;
14131518
14141519
Fixture Loading
14151520
---------------

cookbook/creating_a_cms/getting-started.rst

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ database:
6464
6565
$ php app/console doctrine:database:create
6666
67-
This will create a new database according to the configuration file
67+
This will create a new database according to the configuration file
6868
``parameters.yml``.
6969

7070
The Doctrine DBAL backend needs to be initialized, the following command
@@ -135,31 +135,31 @@ to reduce code duplication::
135135
return $this->id;
136136
}
137137

138-
public function getParent()
138+
public function getParent()
139139
{
140140
return $this->parent;
141141
}
142-
142+
143143
public function setParent($parent)
144144
{
145145
$this->parent = $parent;
146146
}
147-
148-
public function getTitle()
147+
148+
public function getTitle()
149149
{
150150
return $this->title;
151151
}
152-
152+
153153
public function setTitle($title)
154154
{
155155
$this->title = $title;
156156
}
157157

158-
public function getContent()
158+
public function getContent()
159159
{
160160
return $this->content;
161161
}
162-
162+
163163
public function setContent($content)
164164
{
165165
$this->content = $content;
@@ -239,7 +239,7 @@ be referenceable and in addition will automatically set the date using the
239239
}
240240

241241
Both the ``Post`` and ``Page`` classes implement the
242-
``RouteReferrersReadInterface``. This interface enables the
242+
``RouteReferrersReadInterface``. This interface enables the
243243
`DynamicRouter to generate URLs`_ from instances of these classes. (for
244244
example with ``{{ path(content) }}`` in Twig).
245245

@@ -259,9 +259,9 @@ configuration:
259259
260260
# src/Acme/BasicCmsBundle/Resources/config/services.yml
261261
services:
262-
acme.basic_cms.phpcr.initializer:
262+
acme_basiccms.basic_cms.phpcr.initializer:
263263
class: Doctrine\Bundle\PHPCRBundle\Initializer\GenericInitializer
264-
arguments:
264+
arguments:
265265
- ["/cms/pages", "/cms/posts", "/cms/routes"]
266266
tags:
267267
- { name: doctrine_phpcr.initializer }
@@ -273,14 +273,14 @@ configuration:
273273
<container xmlns="http://symfony.com/schema/dic/services"
274274
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
275275
xmlns:acme_demo="http://www.example.com/symfony/schema/"
276-
xsi:schemaLocation="http://symfony.com/schema/dic/services
276+
xsi:schemaLocation="http://symfony.com/schema/dic/services
277277
http://symfony.com/schema/dic/services/services-1.0.xsd">
278278
279279
<!-- ... -->
280280
<services>
281281
<!-- ... -->
282282
283-
<service id="acme.basic_cms.phpcr.initializer"
283+
<service id="acme_basiccms.basic_cms.phpcr.initializer"
284284
class="Doctrine\Bundle\PHPCRBundle\Initializer\GenericInitializer">
285285
286286
<argument type="collection">
@@ -299,7 +299,7 @@ configuration:
299299
// src/Acme/BasicCmsBundle/Resources/config/services.php
300300
$container
301301
->register(
302-
'acme.basic_cms.phpcr.initializer',
302+
'acme_basiccms.basic_cms.phpcr.initializer',
303303
'Doctrine\Bundle\PHPCRBundle\Initializer\GenericInitializer'
304304
)
305305
->addArgument(array('/cms/pages', '/cms/posts', '/cms/routes'))
@@ -339,7 +339,7 @@ Create Data Fixtures
339339
~~~~~~~~~~~~~~~~~~~~
340340

341341
You can use the doctrine data fixtures library to define some initial data for
342-
your CMS.
342+
your CMS.
343343

344344
Ensure that you have the following package installed:
345345

0 commit comments

Comments
 (0)