Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created command for xml mapping entity generator #23

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions EntityGeneratorBundle/Command/MakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace IvozDevTools\EntityGeneratorBundle\Command;

use Symfony\Bundle\MakerBundle\ApplicationAwareMakerInterface;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\FileManager;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\MakerInterface;
use Symfony\Bundle\MakerBundle\Validator;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MakeCommand extends Command
{

private InputConfiguration $inputConfig;
/** @var ConsoleStyle */
private ConsoleStyle $io;
private $checkDependencies = true;

public function __construct(
private MakerInterface $maker,
private FileManager $fileManager,
private Generator $generator
) {
$this->inputConfig = new InputConfiguration();
parent::__construct();
}
protected function configure()
{
$this->maker->configureCommand($this, $this->inputConfig);
}

protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->io = new ConsoleStyle($input, $output);
$this->fileManager->setIO($this->io);

if ($this->checkDependencies) {
$dependencies = new DependencyBuilder();
$this->maker->configureDependencies($dependencies);

if (!$dependencies->isPhpVersionSatisfied()) {
throw new RuntimeCommandException('The make:entity command requires that you use PHP 7.1 or higher.');
}

if ($missingPackagesMessage = $dependencies->getMissingPackagesMessage($this->getName())) {
throw new RuntimeCommandException($missingPackagesMessage);
}
}
}

protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$this->fileManager->isNamespaceConfiguredToAutoload($this->generator->getRootNamespace())) {
$this->io->note([
sprintf('It looks like your app may be using a namespace other than "%s".', $this->generator->getRootNamespace()),
'To configure this and make your life easier, see: https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html#configuration',
]);
}

foreach ($this->getDefinition()->getArguments() as $argument) {
if ($input->getArgument($argument->getName())) {
continue;
}

if (\in_array($argument->getName(), $this->inputConfig->getNonInteractiveArguments(), true)) {
continue;
}

$value = $this->io->ask($argument->getDescription(), $argument->getDefault(), [Validator::class, 'notBlank']);
$input->setArgument($argument->getName(), $value);
}

$this->maker->interact($input, $this->io, $this);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->maker->generate($input, $this->io, $this->generator);

// sanity check for custom makers
if ($this->generator->hasPendingOperations()) {
throw new \LogicException('Make sure to call the writeChanges() method on the generator.');
}

return 0;
}

public function setApplication(Application $application = null)
{
parent::setApplication($application);

if ($this->maker instanceof ApplicationAwareMakerInterface) {
if (null === $application) {
throw new \RuntimeException('Application cannot be null.');
}

$this->maker->setApplication($application);
}
}

/**
* @internal Used for testing commands
*/
public function setCheckDependencies(bool $checkDeps)
{
$this->checkDependencies = $checkDeps;
}

public function getInputConfig(): InputConfiguration
{
return $this->inputConfig;
}

public function setInputConfig(InputConfiguration $inputConfig): self
{
$this->inputConfig = $inputConfig;

return $this;
}
}
122 changes: 12 additions & 110 deletions EntityGeneratorBundle/Command/MakeEntitiesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,126 +2,28 @@

namespace IvozDevTools\EntityGeneratorBundle\Command;

use Symfony\Bundle\MakerBundle\ApplicationAwareMakerInterface;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\MakerInterface;
use Symfony\Bundle\MakerBundle\FileManager;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\MakerInterface;
use Symfony\Bundle\MakerBundle\Validator;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Used as the Command class for the makers.
*
* @internal
*/
final class MakeEntitiesCommand extends Command
final class MakeEntitiesCommand extends MakeCommand
{
protected static $defaultName = 'ivoz:make:entities';

private $maker;
private $fileManager;
private $inputConfig;
/** @var ConsoleStyle */
private $io;
private $checkDependencies = true;
private $generator;

public function __construct(MakerInterface $maker, FileManager $fileManager, Generator $generator)
{
$this->maker = $maker;
$this->fileManager = $fileManager;
$this->inputConfig = new InputConfiguration();
$this->generator = $generator;

parent::__construct();
}

protected function configure()
{
$this->maker->configureCommand($this, $this->inputConfig);
}

protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->io = new ConsoleStyle($input, $output);
$this->fileManager->setIO($this->io);

if ($this->checkDependencies) {
$dependencies = new DependencyBuilder();
$this->maker->configureDependencies($dependencies);

if (!$dependencies->isPhpVersionSatisfied()) {
throw new RuntimeCommandException('The make:entity command requires that you use PHP 7.1 or higher.');
}

if ($missingPackagesMessage = $dependencies->getMissingPackagesMessage($this->getName())) {
throw new RuntimeCommandException($missingPackagesMessage);
}
}
}

protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$this->fileManager->isNamespaceConfiguredToAutoload($this->generator->getRootNamespace())) {
$this->io->note([
sprintf('It looks like your app may be using a namespace other than "%s".', $this->generator->getRootNamespace()),
'To configure this and make your life easier, see: https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html#configuration',
]);
}

foreach ($this->getDefinition()->getArguments() as $argument) {
if ($input->getArgument($argument->getName())) {
continue;
}

if (\in_array($argument->getName(), $this->inputConfig->getNonInteractiveArguments(), true)) {
continue;
}

$value = $this->io->ask($argument->getDescription(), $argument->getDefault(), [Validator::class, 'notBlank']);
$input->setArgument($argument->getName(), $value);
}

$this->maker->interact($input, $this->io, $this);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->maker->generate($input, $this->io, $this->generator);

// sanity check for custom makers
if ($this->generator->hasPendingOperations()) {
throw new \LogicException('Make sure to call the writeChanges() method on the generator.');
}

return 0;
}

public function setApplication(Application $application = null)
{
parent::setApplication($application);

if ($this->maker instanceof ApplicationAwareMakerInterface) {
if (null === $application) {
throw new \RuntimeException('Application cannot be null.');
}

$this->maker->setApplication($application);
}
}

/**
* @internal Used for testing commands
*/
public function setCheckDependencies(bool $checkDeps)
{
$this->checkDependencies = $checkDeps;
public function __construct(
MakerInterface $maker,
FileManager $fileManager,
Generator $generator
) {
parent::__construct(
$maker,
$fileManager,
$generator
);
}
}
Loading
Loading