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

Fix Entity directory does not exist #311

Merged
merged 3 commits into from
Dec 20, 2018
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
14 changes: 14 additions & 0 deletions src/Doctrine/DoctrineHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\MakerBundle\Doctrine;

use Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory;
use Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\ORM\EntityManagerInterface;
Expand Down Expand Up @@ -145,6 +146,19 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected
foreach ($loaded as $m) {
$cmf->setMetadataFor($m->getName(), $m);
}

// Invalidating the cached AnnotationDriver::$classNames to find new Entity classes
$metadataDriver = $em->getConfiguration()->getMetadataDriverImpl();
if ($metadataDriver instanceof MappingDriverChain) {
foreach ($metadataDriver->getDrivers() as $driver) {
if ($driver instanceof AnnotationDriver) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow! Yea, this issue is a bummer - but nice job at least finding a workaround.

Does this only affect the AnnotationDriver?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also for StaticPHPDriver but we only support annotation driver, right?

$classNames = (new \ReflectionObject($driver))->getProperty('classNames');
$classNames->setAccessible(true);
$classNames->setValue($driver, null);
$classNames->setAccessible(false);
}
}
}
}

foreach ($cmf->getAllMetadata() as $m) {
Expand Down
33 changes: 2 additions & 31 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Finder\SplFileInfo;

/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
Expand Down Expand Up @@ -99,20 +98,6 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
return;
}

$entityFinder = $this->fileManager->createFinder('src/Entity/')
// remove if/when we allow entities in subdirectories
->depth('<1')
->name('*.php');
$classes = [];
/** @var SplFileInfo $item */
foreach ($entityFinder as $item) {
if (!$item->getRelativePathname()) {
continue;
}

$classes[] = str_replace(['.php', '/'], ['', '\\'], $item->getRelativePathname());
}

$argument = $command->getDefinition()->getArgument('name');
$question = $this->createEntityClassQuestion($argument->getDescription());
$value = $io->askQuestion($question);
Expand Down Expand Up @@ -362,7 +347,7 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity

// this is a normal field
$data = ['fieldName' => $fieldName, 'type' => $type];
if ('string' == $type) {
if ('string' === $type) {
// default to 255, avoid the question
$data['length'] = $io->ask('Field length', 255, [Validator::class, 'validateLength']);
} elseif ('decimal' === $type) {
Expand Down Expand Up @@ -466,23 +451,9 @@ private function printAvailableTypes(ConsoleStyle $io)

private function createEntityClassQuestion(string $questionText): Question
{
$entityFinder = $this->fileManager->createFinder('src/Entity/')
// remove if/when we allow entities in subdirectories
->depth('<1')
->name('*.php');
$classes = [];
/** @var SplFileInfo $item */
foreach ($entityFinder as $item) {
if (!$item->getRelativePathname()) {
continue;
}

$classes[] = str_replace('/', '\\', str_replace('.php', '', $item->getRelativePathname()));
}

$question = new Question($questionText);
$question->setValidator([Validator::class, 'notBlank']);
$question->setAutocompleterValues($classes);
$question->setAutocompleterValues($this->doctrineHelper->getEntitiesForAutocomplete());

return $question;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>
<one-to-many field="avatars" target-entity="UserAvatar" mapped-by="user" />
</entity>
</doctrine-mapping>
</doctrine-mapping>