Skip to content

Commit c67931f

Browse files
committed
Merge pull request #35 from symfony-cmf/migrator
added a migrator class to make it possible to easily add new pages via the cli
2 parents 211b630 + 021a6fa commit c67931f

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

Migrator/Page.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\SimpleCmsBundle\Migrator;
4+
5+
use Symfony\Component\Yaml\Parser;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
8+
9+
use PHPCR\Util\NodeHelper;
10+
use PHPCR\SessionInterface;
11+
12+
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry;
13+
use Doctrine\Bundle\PHPCRBundle\Migrator\MigratorInterface;
14+
use Doctrine\ODM\PHPCR\DocumentManager;
15+
16+
class Page implements MigratorInterface
17+
{
18+
/**
19+
* @var PHPCR\SessionInterface
20+
*/
21+
protected $session;
22+
23+
/*
24+
* @var Symfony\Component\Console\Output\OutputInterface
25+
*/
26+
protected $output;
27+
28+
/**
29+
* @var \Doctrine\ODM\PHPCR\DocumentManager
30+
*/
31+
protected $dm;
32+
33+
protected $basepath;
34+
35+
protected $dataDir;
36+
37+
public function __construct(ManagerRegistry $registry, $basepath, $dataDir)
38+
{
39+
$this->dm = $registry->getManager();
40+
$this->session = $registry->getConnection();
41+
$this->basepath = $basepath;
42+
$this->dataDir = $dataDir;
43+
}
44+
45+
public function init(SessionInterface $session, OutputInterface $output)
46+
{
47+
$this->session = $session;
48+
$this->output = $output;
49+
}
50+
51+
protected function createPageInstance($className)
52+
{
53+
return new $className(true);
54+
}
55+
56+
public function migrate($path = '/', $depth = -1)
57+
{
58+
if (0 !== strpos($path, $this->basepath)) {
59+
throw new \RuntimeException("The provided identifier '$path' does not start with the base path '{$this->basepath}'");
60+
}
61+
62+
$yaml = new Parser();
63+
$contentPath = substr($path, strlen($this->basepath));
64+
$data = $yaml->parse(file_get_contents($this->dataDir.$contentPath.'.yml'));
65+
66+
NodeHelper::createPath($this->session, preg_replace('#/[^/]*$#', '', $this->basepath));
67+
68+
$class = isset($data['class']) ? $data['class'] : 'Symfony\\Cmf\\Bundle\\SimpleCmsBundle\\Document\\MultilangPage';
69+
70+
$page = $this->dm->find($class, $path);
71+
if (!$page) {
72+
$page = $this->createPageInstance($class);
73+
$page->setId($path);
74+
}
75+
76+
if (isset($data['formats'])) {
77+
$page->setDefault('_format', reset($data['formats']));
78+
$page->setRequirement('_format', implode('|', $data['formats']));
79+
}
80+
81+
if (!empty($data['template'])) {
82+
$page->setDefault(RouteObjectInterface::TEMPLATE_NAME, $data['template']);
83+
}
84+
85+
if (!empty($data['controller'])) {
86+
$page->setDefault(RouteObjectInterface::CONTROLLER_NAME, $data['controller']);
87+
}
88+
89+
if (!empty($data['options'])) {
90+
$page->setOptions($data['options']);
91+
}
92+
93+
$this->dm->persist($page);
94+
95+
if (is_array($data['title'])) {
96+
foreach ($data['title'] as $locale => $title) {
97+
$page->setTitle($title);
98+
if (isset($data['label'][$locale]) && $data['label'][$locale]) {
99+
$page->setLabel($data['label'][$locale]);
100+
} elseif (!isset($data['label'][$locale])) {
101+
$page->setLabel($title);
102+
}
103+
$page->setBody($data['body'][$locale]);
104+
$this->dm->bindTranslation($page, $locale);
105+
}
106+
} else {
107+
$page->setTitle($data['title']);
108+
if (isset($data['label'])) {
109+
if ($data['label']) {
110+
$page->setLabel($data['label']);
111+
}
112+
} elseif (!isset($data['label'])) {
113+
$page->setLabel($data['title']);
114+
}
115+
$page->setBody($data['body']);
116+
}
117+
118+
if (isset($data['create_date'])) {
119+
$page->setCreateDate(date_create_from_format('U', strtotime($data['create_date'])));
120+
}
121+
122+
if (isset($data['publish_start_date'])) {
123+
$page->setPublishStartDate(date_create_from_format('U', strtotime($data['publish_start_date'])));
124+
}
125+
126+
if (isset($data['publish_end_date'])) {
127+
$page->setPublishEndDate(date_create_from_format('U', strtotime($data['publish_end_date'])));
128+
}
129+
130+
$this->dm->flush();
131+
132+
return 0;
133+
}
134+
}

Resources/config/services/routing.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<parameter key="symfony_cmf_simple_cms.enhancer_explicit_template_class">Symfony\Cmf\Component\Routing\Enhancer\FieldPresenceEnhancer</parameter>
1616
<parameter key="symfony_cmf_simple_cms.enhancer_controllers_by_alias_class">Symfony\Cmf\Component\Routing\Enhancer\FieldMapEnhancer</parameter>
1717
<parameter key="symfony_cmf_simple_cms.enhancer_field_by_class_class">Symfony\Cmf\Component\Routing\Enhancer\FieldByClassEnhancer</parameter>
18+
<parameter key="symfony_cmf_simple_cms.migrator_page_class">Symfony\Cmf\Bundle\SimpleCmsBundle\Migrator\Page</parameter>
1819
<parameter key="symfony_cmf_simple_cms.locales" />
1920
</parameters>
2021

@@ -60,5 +61,12 @@
6061
<tag name="doctrine_phpcr.event_listener" event="postPersist" />
6162
</service>
6263

64+
<service id="symfony_cmf_simple_cms.migrator.page" class="%symfony_cmf_simple_cms.migrator_page_class%">
65+
<argument type="service" id="doctrine_phpcr"/>
66+
<argument>%symfony_cmf_simple_cms.basepath%</argument>
67+
<argument>%kernel.root_dir%/Resources/data/pages</argument>
68+
<tag name="doctrine_phpcr.migrator" alias="page" />
69+
</service>
70+
6371
</services>
6472
</container>

0 commit comments

Comments
 (0)