Skip to content

Commit

Permalink
Check if the class exists
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed May 14, 2018
1 parent ae7c296 commit e6ad344
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/Command/MakerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ final class MakerCommand extends Command
/** @var ConsoleStyle */
private $io;
private $checkDependencies = true;
private $generator;

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

parent::__construct();
}
Expand Down Expand Up @@ -83,17 +85,15 @@ protected function interact(InputInterface $input, OutputInterface $output)
$input->setArgument($argument->getName(), $value);
}

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

protected function execute(InputInterface $input, OutputInterface $output)
{
$generator = new Generator($this->fileManager, 'App\\');

$this->maker->generate($input, $this->io, $generator);
$this->maker->generate($input, $this->io, $this->generator);

// sanity check for custom makers
if ($generator->hasPendingOperations()) {
if ($this->generator->hasPendingOperations()) {
throw new \LogicException('Make sure to call the writeChanges() method on the generator.');
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/GeneratorAwareMakerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MakerBundle;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;

/**
* Lets the interact method access to the Generator.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
interface GeneratorAwareMakerInterface extends MakerInterface
{
/**
* {@inheritdoc}
*/
public function interact(InputInterface $input, ConsoleStyle $io, Command $command, Generator $generator = null);
}
11 changes: 8 additions & 3 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\GeneratorAwareMakerInterface;
use Symfony\Bundle\MakerBundle\InputAwareMakerInterface;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Str;
Expand All @@ -42,7 +43,7 @@
* @author Ryan Weaver <weaverryan@gmail.com>
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class MakeEntity extends AbstractMaker implements InputAwareMakerInterface
final class MakeEntity extends AbstractMaker implements InputAwareMakerInterface, GeneratorAwareMakerInterface
{
private $fileManager;
private $doctrineHelper;
Expand Down Expand Up @@ -74,7 +75,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
$inputConf->setArgumentAsNonInteractive('name');
}

public function interact(InputInterface $input, ConsoleStyle $io, Command $command)
public function interact(InputInterface $input, ConsoleStyle $io, Command $command, Generator $generator = null)
{
if ($input->getArgument('name')) {
return;
Expand Down Expand Up @@ -112,7 +113,11 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma

$input->setArgument('name', $value);

if (class_exists(ApiResource::class) && !$input->getOption('api-resource')) {
if (
!$input->getOption('api-resource') &&
class_exists(ApiResource::class) &&
!class_exists($generator->createClassNameDetails($value, 'Entity\\')->getFullName())
) {
$description = $command->getDefinition()->getOption('api-resource')->getDescription();
$question = new ConfirmationQuestion($description, false);
$value = $io->askQuestion($question);
Expand Down

0 comments on commit e6ad344

Please sign in to comment.