Instantly share code, notes, and snippets.

Embed
What would you like to do?
PageSeeder and an implementation
<?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 = '';
}
<?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