Skip to content

added a migrator class to make it possible to easily add new pages via the cli #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions Migrator/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace Symfony\Cmf\Bundle\SimpleCmsBundle\Migrator;

use Symfony\Component\Yaml\Parser;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;

use PHPCR\Util\NodeHelper;
use PHPCR\SessionInterface;

use Doctrine\Bundle\PHPCRBundle\ManagerRegistry;
use Doctrine\Bundle\PHPCRBundle\Migrator\MigratorInterface;
use Doctrine\ODM\PHPCR\DocumentManager;

class Page implements MigratorInterface
{
/**
* @var PHPCR\SessionInterface
*/
protected $session;

/*
* @var Symfony\Component\Console\Output\OutputInterface
*/
protected $output;

/**
* @var \Doctrine\ODM\PHPCR\DocumentManager
*/
protected $dm;

protected $basepath;

protected $dataDir;

public function __construct(ManagerRegistry $registry, $basepath, $dataDir)
{
$this->dm = $registry->getManager();
$this->session = $registry->getConnection();
$this->basepath = $basepath;
$this->dataDir = $dataDir;
}

public function init(SessionInterface $session, OutputInterface $output)
{
$this->session = $session;
$this->output = $output;
}

protected function createPageInstance($className)
{
return new $className(true);
}

public function migrate($path = '/', $depth = -1)
{
if (0 !== strpos($path, $this->basepath)) {
throw new \RuntimeException("The provided identifier '$path' does not start with the base path '{$this->basepath}'");
}

$yaml = new Parser();
$contentPath = substr($path, strlen($this->basepath));
$data = $yaml->parse(file_get_contents($this->dataDir.$contentPath.'.yml'));

NodeHelper::createPath($this->session, preg_replace('#/[^/]*$#', '', $this->basepath));

$class = isset($data['class']) ? $data['class'] : 'Symfony\\Cmf\\Bundle\\SimpleCmsBundle\\Document\\MultilangPage';

$page = $this->dm->find($class, $path);
if (!$page) {
$page = $this->createPageInstance($class);
$page->setId($path);
}

if (isset($data['formats'])) {
$page->setDefault('_format', reset($data['formats']));
$page->setRequirement('_format', implode('|', $data['formats']));
}

if (!empty($data['template'])) {
$page->setDefault(RouteObjectInterface::TEMPLATE_NAME, $data['template']);
}

if (!empty($data['controller'])) {
$page->setDefault(RouteObjectInterface::CONTROLLER_NAME, $data['controller']);
}

if (!empty($data['options'])) {
$page->setOptions($data['options']);
}

$this->dm->persist($page);

if (is_array($data['title'])) {
foreach ($data['title'] as $locale => $title) {
$page->setTitle($title);
if (isset($data['label'][$locale]) && $data['label'][$locale]) {
$page->setLabel($data['label'][$locale]);
} elseif (!isset($data['label'][$locale])) {
$page->setLabel($title);
}
$page->setBody($data['body'][$locale]);
$this->dm->bindTranslation($page, $locale);
}
} else {
$page->setTitle($data['title']);
if (isset($data['label'])) {
if ($data['label']) {
$page->setLabel($data['label']);
}
} elseif (!isset($data['label'])) {
$page->setLabel($data['title']);
}
$page->setBody($data['body']);
}

if (isset($data['create_date'])) {
$page->setCreateDate(date_create_from_format('U', strtotime($data['create_date'])));
}

if (isset($data['publish_start_date'])) {
$page->setPublishStartDate(date_create_from_format('U', strtotime($data['publish_start_date'])));
}

if (isset($data['publish_end_date'])) {
$page->setPublishEndDate(date_create_from_format('U', strtotime($data['publish_end_date'])));
}

$this->dm->flush();

return 0;
}
}
8 changes: 8 additions & 0 deletions Resources/config/services/routing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<parameter key="symfony_cmf_simple_cms.enhancer_explicit_template_class">Symfony\Cmf\Component\Routing\Enhancer\FieldPresenceEnhancer</parameter>
<parameter key="symfony_cmf_simple_cms.enhancer_controllers_by_alias_class">Symfony\Cmf\Component\Routing\Enhancer\FieldMapEnhancer</parameter>
<parameter key="symfony_cmf_simple_cms.enhancer_field_by_class_class">Symfony\Cmf\Component\Routing\Enhancer\FieldByClassEnhancer</parameter>
<parameter key="symfony_cmf_simple_cms.migrator_page_class">Symfony\Cmf\Bundle\SimpleCmsBundle\Migrator\Page</parameter>
<parameter key="symfony_cmf_simple_cms.locales" />
</parameters>

Expand Down Expand Up @@ -60,5 +61,12 @@
<tag name="doctrine_phpcr.event_listener" event="postPersist" />
</service>

<service id="symfony_cmf_simple_cms.migrator.page" class="%symfony_cmf_simple_cms.migrator_page_class%">
<argument type="service" id="doctrine_phpcr"/>
<argument>%symfony_cmf_simple_cms.basepath%</argument>
<argument>%kernel.root_dir%/Resources/data/pages</argument>
<tag name="doctrine_phpcr.migrator" alias="page" />
</service>

</services>
</container>