Created
February 21, 2018 18:39
-
-
Save squatto/ed23fc0cc7574e7a5a3c4945173890c0 to your computer and use it in GitHub Desktop.
PageSeeder and an implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Freemotion\FreemotionTheme\Database\Seeder; | |
class AboutPageSeeder extends PageSeeder | |
{ | |
protected $type = 'about'; | |
protected $title = 'About Freemotion Fitness'; | |
protected $slug = 'about'; | |
protected $enabled = true; | |
protected $content = ''; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Freemotion\FreemotionTheme\Database\Seeder; | |
use Anomaly\Streams\Platform\Database\Seeder\Seeder; | |
use Anomaly\PagesModule\Page\Contract\PageRepositoryInterface; | |
use Anomaly\PagesModule\Type\Contract\TypeRepositoryInterface; | |
class PageSeeder extends Seeder | |
{ | |
/** | |
* The page repository. | |
* | |
* @var PageRepositoryInterface | |
*/ | |
protected $pageRepository; | |
/** | |
* The type repository. | |
* | |
* @var TypeRepositoryInterface | |
*/ | |
protected $typeRepository; | |
// configuration | |
protected $type = ''; | |
protected $title = ''; | |
protected $slug = ''; | |
protected $content = null; | |
protected $enabled = true; | |
protected $home = false; | |
protected $layout = ''; | |
/** | |
* Create a new PageSeeder instance. | |
* | |
* @param PageRepositoryInterface $pageRepository | |
* @param TypeRepositoryInterface $typeRepository | |
*/ | |
public function __construct(PageRepositoryInterface $pageRepository, TypeRepositoryInterface $typeRepository) | |
{ | |
$this->pageRepository = $pageRepository; | |
$this->typeRepository = $typeRepository; | |
} | |
/** | |
* Run the seeder. | |
*/ | |
public function run() | |
{ | |
$this->checkRequired(); | |
if ($page = $this->pageRepository->findBy('slug', $this->slug)) { | |
// you MUST call forceDelete() or you will get integrity constraint violations | |
$this->pageRepository->forceDelete($page); | |
} | |
$type = $this->typeRepository->findBySlug($this->type); | |
if (! $type) { | |
throw new \Exception("Page type not found: $this->type"); | |
} | |
$data = [ | |
'en' => [ | |
'title' => $this->title, | |
], | |
'slug' => $this->slug, | |
'type' => $type, | |
'enabled' => $this->enabled, | |
'home' => $this->home, | |
'theme_layout' => $this->layout ?: 'theme::layouts/default.twig', | |
]; | |
if ($this->content) { | |
$data['entry'] = $type->getEntryModel()->create([ | |
'en' => [ | |
'content' => $this->content, | |
], | |
]); | |
} | |
$this->pageRepository->create($data)->allowedRoles()->sync([]); | |
} | |
private function checkRequired() | |
{ | |
if (! $this->type || ! $this->title || ! $this->slug) { | |
throw new \Exception('Required page seeder values were not set'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment