Last active
July 22, 2017 04:05
-
-
Save Piterden/de51ec7ac571a53d7cd9800d726d8920 to your computer and use it in GitHub Desktop.
Seeder example
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 Defr\FtlTheme\Page\Command; | |
use Anomaly\FilesModule\Disk\Contract\DiskRepositoryInterface; | |
use Anomaly\FilesModule\Folder\Contract\FolderRepositoryInterface; | |
use Anomaly\PagesModule\Page\Contract\PageInterface; | |
use Anomaly\Streams\Platform\Model\Pages\PagesEventPagesEntryModel; | |
class CreateFolder | |
{ | |
/** | |
* Page model | |
* | |
* @var mixed | |
*/ | |
protected $page; | |
/** | |
* Create an instance of CreateFolder class | |
* | |
* @param PageInterface $page The page | |
*/ | |
public function __construct(PageInterface $page) | |
{ | |
$this->page = $page; | |
} | |
/** | |
* Handle the command | |
* | |
* @param FolderRepositoryInterface $folders The folders | |
* @param DiskRepositoryInterface $disks The disks | |
*/ | |
public function handle( | |
FolderRepositoryInterface $folders, | |
DiskRepositoryInterface $disks | |
) | |
{ | |
if ($this->page->getEntry() instanceof PagesEventPagesEntryModel) | |
{ | |
$disk = $disks->findBySlug('local'); | |
$slug = $this->page->getSlug(); | |
$title = $this->page->getTitle(); | |
$hash = md5($slug); | |
$folders->create([ | |
'en' => [ | |
'name' => $title, | |
'description' => "A folder for {$title}.", | |
], | |
'slug' => $hash, | |
'disk' => $disk, | |
'allowed_types' => [ | |
'png', | |
'jpeg', | |
'jpg', | |
'gif', | |
'svg', | |
], | |
]); | |
} | |
} | |
} |
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 Defr\FtlTheme\Page; | |
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface; | |
use Defr\FtlTheme\Page\Command\CreateFolder; | |
class PageObserver extends \Anomaly\PagesModule\Page\PageObserver | |
{ | |
/** | |
* Fired after saving the page. | |
* | |
* @param EntryInterface|PageInterface|EntryModel $entry | |
*/ | |
public function created(EntryInterface $entry) | |
{ | |
parent::created($entry); | |
$this->dispatch(new CreateFolder($entry)); | |
} | |
} |
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 Defr\FtlTheme\Page; | |
use Anomaly\FilesModule\File\Contract\FileRepositoryInterface; | |
use Anomaly\FilesModule\File\FileUploader; | |
use Anomaly\FilesModule\Folder\Contract\FolderRepositoryInterface; | |
use Anomaly\PagesModule\Page\Contract\PageRepositoryInterface; | |
use Anomaly\PagesModule\Type\Contract\TypeRepositoryInterface; | |
use Illuminate\Filesystem\Filesystem; | |
use Illuminate\Http\UploadedFile; | |
class PageSeeder extends \Anomaly\PagesModule\Page\PageSeeder | |
{ | |
/** | |
* The files repository. | |
* | |
* @var FileRepositoryInterface | |
*/ | |
protected $files; | |
/** | |
* Uploader | |
* | |
* @var FileUploader | |
*/ | |
protected $uploader; | |
/** | |
* The folders repository. | |
* | |
* @var FolderRepositoryInterface | |
*/ | |
protected $folders; | |
/** | |
* Create a new ProductSeeder instance. | |
* | |
* @param Filesystem $filesystem The filesystem | |
* @param FileUploader $uploader The uploader | |
* @param PageRepositoryInterface $pages The pages | |
* @param TypeRepositoryInterface $types The types | |
* @param Filesystem $files The files | |
* @param FolderRepositoryInterface $folders The folders | |
*/ | |
public function __construct( | |
Filesystem $filesystem, | |
FileUploader $uploader, | |
PageRepositoryInterface $pages, | |
TypeRepositoryInterface $types, | |
FileRepositoryInterface $files, | |
FolderRepositoryInterface $folders | |
) | |
{ | |
parent::__construct($pages, $types); | |
$this->files = $files; | |
$this->folders = $folders; | |
$this->uploader = $uploader; | |
$this->filesystem = $filesystem; | |
} | |
/** | |
* Run the seeder | |
*/ | |
public function run() | |
{ | |
echo "\n\033[37;5;228mStarting pages seeder!\n"; | |
$this->pages->truncate(); | |
$this->files->truncate(); | |
$this->folders->truncate(); | |
echo "\033[35;5;228mPages truncated!\n"; | |
$data = include_once __DIR__ . '/../../resources/seeder/modx_site_content.php'; | |
$home = true; | |
foreach ($data as $item) | |
{ | |
$title = array_get($item, 'pagetitle', ''); | |
$slug = array_get($item, 'alias', ''); | |
$content = array_get($item, 'content', ''); | |
$parent = array_get($item, 'parent', ''); | |
$summary = array_get($item, 'introtext', ''); | |
$type = ($parent == '2') ? $this->types->findBySlug('event') : $this->types->findBySlug('default'); | |
$entry = $type->getEntryModel()->create([ | |
'en' => [ | |
'content' => $content, | |
], | |
'ru' => [ | |
'content' => $content, | |
], | |
]); | |
$this->pages->create([ | |
'ru' => [ | |
'title' => $title, | |
'meta_title' => $title, | |
'meta_description' => $summary, | |
], | |
'en' => [ | |
'title' => $title, | |
'meta_title' => $title, | |
'meta_description' => $summary, | |
], | |
'slug' => $slug, | |
'entry' => $entry, | |
'type' => $type, | |
'enabled' => true, | |
'home' => $home, | |
'parent' => $parent, | |
'theme_layout' => 'theme::layouts/default.twig', | |
])->allowedRoles()->sync([]); | |
$home = false; | |
echo "\033[36;5;228mCreated page \033[31;5;228m{$title}. Slug: {$slug}\n"; | |
if ($parent == '2') | |
{ | |
$hash = md5($slug); | |
if ($folder = $this->folders->findBySlug($hash)) | |
{ | |
$images = $this->filesystem->glob(__DIR__ . '/../../resources/seeder/gallery/' . $slug . '/*.jpg'); | |
$count = count($images); | |
echo "\033[31;5;228mFound {$count} images. \033[36;5;228mStarting images upload!\n"; | |
$i = 1; | |
$ids = []; | |
foreach ($images as $image) | |
{ | |
$uploaded = new UploadedFile( | |
$image, | |
$slug . '_' . $i . '.jpg', | |
'image/jpeg', | |
filesize($image), | |
null, | |
true | |
); | |
$file = $this->uploader->upload($uploaded, $folder); | |
$ids[] = $file->getId(); | |
++$i; | |
echo "\033[31;5;228mUploaded {$image}.\n"; | |
} | |
echo "\033[36;5;228mAll images was uploaded!\n"; | |
$entry->gallery()->sync($ids); | |
$entry->save(); | |
} | |
} | |
} | |
echo "\033[32;5;228mPages was seeded successfully!\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment