Skip to content

Commit

Permalink
closed #3
Browse files Browse the repository at this point in the history
  • Loading branch information
artgris committed Sep 22, 2019
1 parent 849bdd2 commit 8851d72
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ Import model ("/pages/model.yaml") in database
add *--remove-deviants* to delete the content of types that have changed (ex: text type in yaml but integer type in bd)
php bin/console artgris:page:import --remove-deviants
add *--ignore-names* to ignore page and bloc names that have changed
php bin/console artgris:page:import --ignore-names
Remove extra pages/blocks (in database but not in model.yaml)
php bin/console artgris:page:remove:extra
Expand Down
6 changes: 3 additions & 3 deletions src/Command/ExportModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public function execute(InputInterface $input, OutputInterface $output)
$yaml = Yaml::dump($pages, 5);

$filesystem = new Filesystem();
$dirName = $this->kernel->getProjectDir() . self::DIRNAME;
$fileName = $dirName . self::FILENAME;
$dirName = $this->kernel->getProjectDir().self::DIRNAME;
$fileName = $dirName.self::FILENAME;
try {
$filesystem->mkdir($dirName);
} catch (IOExceptionInterface $exception) {
$io->error('An error occurred while creating your directory at' . $exception->getPath());
$io->error('An error occurred while creating your directory at'.$exception->getPath());
}

if ($filesystem->exists($fileName)) {
Expand Down
43 changes: 31 additions & 12 deletions src/Command/ImportModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
*/
class ImportModelCommand extends Command
{
private const REMOVE_DEVIANTS = 'remove-deviants';
protected static $defaultName = 'artgris:page:import';

private const REMOVE_DEVIANTS = 'remove-deviants';
private const IGNORE_NAMES = 'ignore-names';

private const BLOCK_FIELDS = ['name', 'position', 'translatable', 'type'];
private const PAGE_FIELDS = ['name', 'route'];
/**
* @var KernelInterface
*/
Expand All @@ -43,7 +48,9 @@ public function __construct(KernelInterface $kernel, EntityManagerInterface $em)
protected function configure()
{
$this
->addOption(self::REMOVE_DEVIANTS, null, InputOption::VALUE_NONE, 'Delete the content of types that have changed');
->addOption(self::REMOVE_DEVIANTS, null, InputOption::VALUE_NONE, 'Delete the content of types that have changed')
->addOption(self::IGNORE_NAMES, null, InputOption::VALUE_NONE, 'Ignore names that have changed'
);
}

public function execute(InputInterface $input, OutputInterface $output)
Expand All @@ -59,6 +66,12 @@ public function execute(InputInterface $input, OutputInterface $output)
$io->title('Operations found:');

$operations = [];
$blockFields = self::BLOCK_FIELDS;
$pageFields = self::PAGE_FIELDS;

if ($input->getOption(self::IGNORE_NAMES)) {
unset($blockFields['name'], $pageFields['name']);
}

foreach ($pages as $pageSlug => $page) {
$originalPageEntity = null;
Expand All @@ -71,9 +84,11 @@ public function execute(InputInterface $input, OutputInterface $output)
$originalPageEntity = clone $pageEntity;
}
$pageEntity->setRoute($page['route']);
$pageEntity->setName($page['name']);

if ($originalPageEntity !== null && !empty($fields = $this->comparePages($originalPageEntity, $pageEntity))) {
if ($originalPageEntity === null || ($originalPageEntity && !$input->getOption(self::IGNORE_NAMES))) {
$pageEntity->setName($page['name']);
}
if ($originalPageEntity !== null && !empty($fields = $this->comparePages($originalPageEntity, $pageEntity, $pageFields))) {
$operations[] = "<fg=default;bg=yellow>Edit page '" . $pageSlug . "' (" . implode(',', $fields) . ')</>';
}

Expand All @@ -93,16 +108,21 @@ public function execute(InputInterface $input, OutputInterface $output)

$blockEntity->setPage($pageEntity);
$blockEntity->setPosition($position);
$blockEntity->setName($block['name']);

$requiredNameUpdate = $originalBlockEntity === null || ($originalBlockEntity && !$input->getOption(self::IGNORE_NAMES));

if ($requiredNameUpdate) {
$blockEntity->setName($block['name']);
}

$blockEntity->setTranslatable($block['translatable']);
$blockEntity->setType($block['type']);


if ($originalBlockEntity !== null && !empty($fields = $this->compareBlocks($originalBlockEntity, $blockEntity))) {
if ($originalBlockEntity !== null && !empty($fields = $this->compareBlocks($originalBlockEntity, $blockEntity, $blockFields))) {
$operations[] = "<fg=default;bg=yellow>Edit block '" . $blockSlug . "' (" . implode(',', $fields) . ')</>';

if ($blockEntity->getType() !== $originalBlockEntity->getType()) {

if ($input->getOption(self::REMOVE_DEVIANTS)) {
$blockEntity->setContent(null);
// delete translations
Expand All @@ -121,7 +141,6 @@ public function execute(InputInterface $input, OutputInterface $output)
}

if (!empty($operations)) {

$io->listing($operations);

if (!$io->confirm('Executes the queries(flush) ?')) {
Expand All @@ -138,18 +157,18 @@ public function execute(InputInterface $input, OutputInterface $output)
}
}

private function comparePages(ArtgrisPage $origin, ArtgrisPage $update)
private function comparePages(ArtgrisPage $origin, ArtgrisPage $update, array $fields)
{
$metaData = $this->em->getClassMetadata(ArtgrisPage::class);

return $this->compareEntity($origin, $update, $metaData, ['name', 'route']);
return $this->compareEntity($origin, $update, $metaData, $fields);
}

private function compareBlocks(ArtgrisBlock $origin, ArtgrisBlock $update)
private function compareBlocks(ArtgrisBlock $origin, ArtgrisBlock $update, array $fields)
{
$metaData = $this->em->getClassMetadata(ArtgrisBlock::class);

return $this->compareEntity($origin, $update, $metaData, ['name', 'position', 'translatable', 'type']);
return $this->compareEntity($origin, $update, $metaData, $fields);
}

private function compareEntity($origin, $update, ClassMetadataInfo $metaData, array $fields)
Expand Down
4 changes: 1 addition & 3 deletions src/Command/RemoveExtraCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);

$fileName = $this->kernel->getProjectDir() . ExportModelCommand::DIRNAME . ExportModelCommand::FILENAME;
$fileName = $this->kernel->getProjectDir().ExportModelCommand::DIRNAME.ExportModelCommand::FILENAME;
$pages = Yaml::parseFile($fileName);

$extraPages = $this->em->getRepository(ArtgrisPage::class)->findPageDiff(array_keys($pages));
Expand All @@ -65,7 +65,6 @@ public function execute(InputInterface $input, OutputInterface $output)
$io->success('No page to delete');
}


$blocks = [];
foreach ($pages as $page) {
foreach ($page['blocks'] as $blockSlug => $block) {
Expand Down Expand Up @@ -95,6 +94,5 @@ public function execute(InputInterface $input, OutputInterface $output)
} else {
$io->success('No block to delete');
}

}
}

0 comments on commit 8851d72

Please sign in to comment.