Skip to content

Instantly share code, notes, and snippets.

@Neodork
Last active March 2, 2017 14:23
Show Gist options
  • Save Neodork/bbfb1b5d305f22a5a835 to your computer and use it in GitHub Desktop.
Save Neodork/bbfb1b5d305f22a5a835 to your computer and use it in GitHub Desktop.
Global page migration - Sonata Project
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
use Sonata\PageBundle\Model\SiteInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Add a site, and the global page.
*/
class Version20151219141914 extends AbstractMigration implements ContainerAwareInterface
{
const SITE_NAME = 'localhost';
const SITE_HOST = 'localhost';
const SITE_RELATIVE_PATH = '';
const SITE_IS_ENABLED = true;
const SITE_IS_DEFAULT = true;
private $container;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
$site = $this->createSite();
$this->createGlobalPage($site);
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
$this->removeGlobalPage();
$this->removeSite();
}
/**
* Remove the global page if it exists
*/
private function removeGlobalPage()
{
$pageManager = $this->getPageManager();
$global = $pageManager->findOneBy(array('name' => 'global'));
//Page was somehow already removed
if(!$global){
return;
}
$pageManager->delete($global);
}
/**
* Remove the site if it exists
*/
private function removeSite()
{
$siteManager = $this->getSiteManager();
$site = $siteManager->findOneBy(array('name' => self::SITE_NAME));
// Site was somehow already removed
if(!$site){
return;
}
$this->getSiteManager()->delete($site);
}
/**
* @return SiteInterface $site
*/
private function createSite()
{
$site = $this->getSiteManager()->create();
$site->setHost(self::SITE_HOST);
$site->setEnabled(self::SITE_IS_ENABLED);
$site->setName(self::SITE_NAME);
$site->setEnabledFrom(new \DateTime('now'));
$site->setEnabledTo(new \DateTime('+10 years'));
$site->setRelativePath(self::SITE_RELATIVE_PATH);
$site->setIsDefault(self::SITE_IS_DEFAULT);
$this->getSiteManager()->save($site);
return $site;
}
/**
* @param SiteInterface $site
*/
private function createGlobalPage(SiteInterface $site)
{
$pageManager = $this->getPageManager();
$global = $pageManager->create();
$global->setName('global');
$global->setRouteName('_page_internal_global');
$global->setSite($site);
$pageManager->save($global);
}
/**
* @return \Sonata\PageBundle\Model\SiteManagerInterface
*/
private function getSiteManager()
{
return $this->container->get('sonata.page.manager.site');
}
/**
* @return \Sonata\PageBundle\Model\PageManagerInterface
*/
private function getPageManager()
{
return $this->container->get('sonata.page.manager.page');
}
}
@Neodork
Copy link
Author

Neodork commented Mar 2, 2017

Wouldn't use this, sonata does this under the hood now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment