Created
October 19, 2018 15:05
-
-
Save mops1k/18617e4d9957e20d2058836d4cf3c2ea to your computer and use it in GitHub Desktop.
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 declare(strict_types=1); | |
namespace App\Command; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Console\Style\SymfonyStyle; | |
class GenerateEntityToArrayCommand extends ContainerAwareCommand | |
{ | |
/** @var string */ | |
private $fullEntityName; | |
/** @var string */ | |
protected static $defaultName = 'generate:entity:to_array'; | |
/** | |
* configure | |
*/ | |
protected function configure() | |
{ | |
$this | |
->setDescription('Generate entity simple fields without assotiation toArray method') | |
->addArgument('entityName', InputArgument::REQUIRED, 'Entity name') | |
; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$io = new SymfonyStyle($input, $output); | |
$result = $this->getArrayStringForEntity($input->getArgument('entityName')); | |
if (!$result) { | |
$io->error('Entity not found'); | |
return; | |
} | |
$io->writeln($result); | |
$file = __DIR__ . str_replace(['\\', 'App'], ['/', '/..'], $this->fullEntityName) . '.php'; | |
if (!file_exists($file)) { | |
$io->error("File ${file} not found!"); | |
return; | |
} | |
$content = file_get_contents($file); | |
$newContent = $this->getNewContent($content, $result); | |
file_put_contents($file, $newContent); | |
} | |
/** | |
* findFullEntityName | |
* | |
* @param string $entityName | |
* | |
* @return bool|null | |
*/ | |
private function findFullEntityName(string $entityName) | |
{ | |
$em = $this->getContainer()->get('doctrine')->getManager(); | |
$entityClasses[] = $em->getConfiguration()->getMetadataDriverImpl()->getAllClassNames(); | |
$class = null; | |
foreach ($entityClasses[0] as $item) { | |
$last = explode("\\", $item); | |
if ($entityName == end($last)) { | |
$class = $item; | |
unset($entityClasses); | |
break; | |
} | |
} | |
if (!$class) { | |
return false; | |
} | |
$this->fullEntityName = $class; | |
return $class; | |
} | |
/** | |
* getArrayStringForEntity | |
* | |
* @param string $entityName | |
* | |
* @return bool|string | |
*/ | |
private function getArrayStringForEntity(string $entityName) | |
{ | |
$em = $this->getContainer()->get('doctrine')->getManager(); | |
$class = $this->fullEntityName ?: $this->findFullEntityName($entityName); | |
if (!$class) { | |
return false; | |
} | |
$metadata = $em->getClassMetadata($class); | |
$fieldNames = $metadata->getFieldNames(); | |
$result = "\t\treturn ["; | |
foreach ($fieldNames as $fieldName) { | |
if ($metadata->hasAssociation($fieldName)) { | |
continue; | |
} | |
$type = $metadata->getTypeOfField($fieldName); | |
$value = "\$this->${fieldName}"; | |
if (in_array($type, ['date', 'datetime'])) { | |
$value = "\$this->${fieldName} ? \$this->${fieldName}->format('c') : null"; | |
} | |
$result .= "\n\t\t\t'${fieldName}' => ${value},"; | |
} | |
$result .= "\n\t\t];"; | |
return $result; | |
} | |
/** | |
* getNewContent | |
* | |
* @param string $content | |
* @param string $data | |
* | |
* @return null|string|string[] | |
*/ | |
private function getNewContent(string $content, string $data) | |
{ | |
$regexp = '/public\sfunction\stoArray\(\)(\:\s?array)?\s*\{\s([\[\]\s\w\\\'\"\-\_\!\=\>\$\,\?\:\(\)\;\@]*)\s{5}\}/i'; | |
$newContent = null; | |
if (preg_match($regexp, $content)) { | |
$newContent = preg_replace_callback( | |
$regexp, | |
function ($matches) use ($data) { | |
return str_replace($matches[2], $data, $matches[0]); | |
}, | |
$content | |
); | |
} | |
if (!$newContent) { | |
$newContent = preg_replace_callback( | |
'/\s{4}\}(\s*)\}/i', | |
function ($matches) use ($data) { | |
$startMethod = "\n\n\t/**\n\t * @return array\n\t */\n\tpublic function toArray(): array\n\t{\n"; | |
$result = $startMethod . $data . "\n\t}\n"; | |
return str_replace($matches[1], $result, $matches[0]); | |
}, | |
$content | |
); | |
} | |
return str_replace("\t", " ", $newContent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment