Skip to content

Commit

Permalink
Merge branch 'main' into collection-var-typehint
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rdex authored Jan 28, 2023
2 parents 94a6d14 + 7fe9fc1 commit d1747f0
Show file tree
Hide file tree
Showing 128 changed files with 1,726 additions and 382 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## [v1.48.0](https://github.com/symfony/maker-bundle/releases/tag/v1.48.0)

*November 14th, 2022*

### Feature

- [#1221](https://github.com/symfony/maker-bundle/pull/1221) - [make:voter] Set type for subject in Voter template - *@N-M*
### Bug

- [#1232](https://github.com/symfony/maker-bundle/pull/1232) - [make:entity] Minor: Consistent output formatting - *@ThomasLandauer*
- [#1227](https://github.com/symfony/maker-bundle/pull/1227) - [make:registration] Make router optional in MakeRegistrationForm constructor - *@odolbeau*
- [#1226](https://github.com/symfony/maker-bundle/pull/1226) - [make:controller] replace repository method add by save - *@bechir*

## [v1.47.0](https://github.com/symfony/maker-bundle/releases/tag/v1.47.0)

*October 4th, 2022*
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"symfony/filesystem": "^5.4.7|^6.0",
"symfony/finder": "^5.4.3|^6.0",
"symfony/framework-bundle": "^5.4.7|^6.0",
"symfony/http-kernel": "^5.4.7|^6.0"
"symfony/http-kernel": "^5.4.7|^6.0",
"symfony/process": "^5.4.7|^6.0"
},
"require-dev": {
"composer/semver": "^3.0",
Expand All @@ -32,7 +33,6 @@
"symfony/http-client": "^5.4.7|^6.0",
"symfony/phpunit-bridge": "^5.4.7|^6.0",
"symfony/polyfill-php80": "^1.16.0",
"symfony/process": "^5.4.7|^6.0",
"symfony/security-core": "^5.4.7|^6.0",
"symfony/yaml": "^5.4.3|^6.0",
"twig/twig": "^2.0|^3.0"
Expand Down
15 changes: 13 additions & 2 deletions src/Command/MakerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\MakerInterface;
use Symfony\Bundle\MakerBundle\Util\TemplateLinter;
use Symfony\Bundle\MakerBundle\Validator;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
Expand All @@ -36,8 +37,12 @@ final class MakerCommand extends Command
private ConsoleStyle $io;
private bool $checkDependencies = true;

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

parent::__construct();
Expand Down Expand Up @@ -90,13 +95,19 @@ protected function interact(InputInterface $input, OutputInterface $output): voi

protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($output->isVerbose()) {
$this->linter->writeLinterMessage($output);
}

$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.');
}

$this->linter->lintFiles($this->generator->getGeneratedFiles());

return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/EntityRegenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private function createClassManipulator(string $classPath): ClassSourceManipulat
sourceCode: $this->fileManager->getFileContents($classPath),
overwrite: $this->overwrite,
// if properties need to be generated then, by definition,
// some non-annotation config is being used, and so, the
// some non-annotation config is being used (e.g. XML), and so, the
// properties should not have annotations added to them
useAttributesForDoctrineMapping: false
);
Expand Down
64 changes: 40 additions & 24 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Generator
private GeneratorTwigHelper $twigHelper;
private array $pendingOperations = [];
private ?TemplateComponentGenerator $templateComponentGenerator;
private array $generatedFiles = [];

public function __construct(
private FileManager $fileManager,
Expand Down Expand Up @@ -74,6 +75,8 @@ public function generateClass(string $className, string $templateName, array $va

/**
* Generate a normal file from a template.
*
* @return void
*/
public function generateFile(string $targetPath, string $templateName, array $variables = [])
{
Expand All @@ -84,6 +87,9 @@ public function generateFile(string $targetPath, string $templateName, array $va
$this->addOperation($targetPath, $templateName, $variables);
}

/**
* @return void
*/
public function dumpFile(string $targetPath, string $contents)
{
$this->pendingOperations[$targetPath] = [
Expand Down Expand Up @@ -160,29 +166,6 @@ public function getRootDirectory(): string
return $this->fileManager->getRootDirectory();
}

private function addOperation(string $targetPath, string $templateName, array $variables): void
{
if ($this->fileManager->fileExists($targetPath)) {
throw new RuntimeCommandException(sprintf('The file "%s" can\'t be generated because it already exists.', $this->fileManager->relativizePath($targetPath)));
}

$variables['relative_path'] = $this->fileManager->relativizePath($targetPath);

$templatePath = $templateName;
if (!file_exists($templatePath)) {
$templatePath = __DIR__.'/Resources/skeleton/'.$templateName;

if (!file_exists($templatePath)) {
throw new \Exception(sprintf('Cannot find template "%s"', $templateName));
}
}

$this->pendingOperations[$targetPath] = [
'template' => $templatePath,
'variables' => $variables,
];
}

public function hasPendingOperations(): bool
{
return !empty($this->pendingOperations);
Expand All @@ -196,6 +179,8 @@ public function hasPendingOperations(): bool
public function writeChanges()
{
foreach ($this->pendingOperations as $targetPath => $templateData) {
$this->generatedFiles[] = $targetPath;

if (isset($templateData['contents'])) {
$this->fileManager->dumpFile($targetPath, $templateData['contents']);

Expand All @@ -204,7 +189,7 @@ public function writeChanges()

$this->fileManager->dumpFile(
$targetPath,
$this->getFileContentsForPendingOperation($targetPath, $templateData)
$this->getFileContentsForPendingOperation($targetPath)
);
}

Expand Down Expand Up @@ -242,6 +227,14 @@ public function generateTemplate(string $targetPath, string $templateName, array
);
}

/**
* Get the full path of each file created by the Generator.
*/
public function getGeneratedFiles(): array
{
return $this->generatedFiles;
}

/**
* @deprecated MakerBundle only supports AbstractController::class. This method will be removed in the future.
*/
Expand All @@ -251,4 +244,27 @@ public static function getControllerBaseClass(): ClassNameDetails

return new ClassNameDetails(AbstractController::class, '\\');
}

private function addOperation(string $targetPath, string $templateName, array $variables): void
{
if ($this->fileManager->fileExists($targetPath)) {
throw new RuntimeCommandException(sprintf('The file "%s" can\'t be generated because it already exists.', $this->fileManager->relativizePath($targetPath)));
}

$variables['relative_path'] = $this->fileManager->relativizePath($targetPath);

$templatePath = $templateName;
if (!file_exists($templatePath)) {
$templatePath = __DIR__.'/Resources/skeleton/'.$templateName;

if (!file_exists($templatePath)) {
throw new \Exception(sprintf('Cannot find template "%s"', $templateName));
}
}

$this->pendingOperations[$targetPath] = [
'template' => $templatePath,
'variables' => $variables,
];
}
}
2 changes: 1 addition & 1 deletion src/Maker/MakeAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
{
if (!$this->fileManager->fileExists($path = 'config/packages/security.yaml')) {
throw new RuntimeCommandException('The file "config/packages/security.yaml" does not exist. This command requires that file to exist so that it can be updated.');
throw new RuntimeCommandException('The file "config/packages/security.yaml" does not exist. PHP & XML configuration formats are currently not supported.');
}
$manipulator = new YamlSourceManipulator($this->fileManager->getFileContents($path));
$securityData = $manipulator->getData();
Expand Down
11 changes: 0 additions & 11 deletions src/Maker/MakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Bundle\MakerBundle\Maker;

use Doctrine\Common\Annotations\Annotation;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
Expand All @@ -27,7 +26,6 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Annotation\Route;

/**
Expand Down Expand Up @@ -115,15 +113,6 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen

public function configureDependencies(DependencyBuilder $dependencies): void
{
// @legacy - Remove method when support for Symfony 5.4 is dropped
if (null !== $this->phpCompatUtil && 60000 <= Kernel::VERSION_ID) {
return;
}

$dependencies->addClassDependency(
Annotation::class,
'doctrine/annotations'
);
}

private function isTwigInstalled(): bool
Expand Down
5 changes: 4 additions & 1 deletion src/Maker/MakeCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
Expand Down Expand Up @@ -317,9 +318,11 @@ public function configureDependencies(DependencyBuilder $dependencies): void
'security-csrf'
);

// @legacy - Remove dependency when support for Symfony <6.2 is dropped
$dependencies->addClassDependency(
ParamConverter::class,
'annotations'
'annotations',
!class_exists(EntityValueResolver::class) // sensio/framework-extra-bundle dependency is not required when using symfony 6.2+
);
}
}
13 changes: 7 additions & 6 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Util\ClassDetails;
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
use Symfony\Bundle\MakerBundle\Util\CliOutputHelper;
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;
use Symfony\Bundle\MakerBundle\Validator;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -310,7 +311,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen

$this->writeSuccessMessage($io);
$io->text([
'Next: When you\'re ready, create a migration with <info>php bin/console make:migration</info>',
sprintf('Next: When you\'re ready, create a migration with <info>%s make:migration</info>', CliOutputHelper::getCommandPrefix()),
'',
]);
}
Expand Down Expand Up @@ -502,10 +503,10 @@ private function printAvailableTypes(ConsoleStyle $io): void
$io->writeln('');
};

$io->writeln('<info>Main types</info>');
$io->writeln('<info>Main Types</info>');
$printSection($typesTable['main']);

$io->writeln('<info>Relationships / Associations</info>');
$io->writeln('<info>Relationships/Associations</info>');
$printSection($typesTable['relation']);

$io->writeln('<info>Array/Object Types</info>');
Expand Down Expand Up @@ -765,17 +766,17 @@ private function askRelationType(ConsoleStyle $io, string $entityClass, string $
$rows = [];
$rows[] = [
EntityRelation::MANY_TO_ONE,
sprintf("Each <comment>%s</comment> relates to (has) <info>one</info> <comment>%s</comment>.\nEach <comment>%s</comment> can relate to (can have) <info>many</info> <comment>%s</comment> objects", $originalEntityShort, $targetEntityShort, $targetEntityShort, $originalEntityShort),
sprintf("Each <comment>%s</comment> relates to (has) <info>one</info> <comment>%s</comment>.\nEach <comment>%s</comment> can relate to (can have) <info>many</info> <comment>%s</comment> objects.", $originalEntityShort, $targetEntityShort, $targetEntityShort, $originalEntityShort),
];
$rows[] = ['', ''];
$rows[] = [
EntityRelation::ONE_TO_MANY,
sprintf("Each <comment>%s</comment> can relate to (can have) <info>many</info> <comment>%s</comment> objects.\nEach <comment>%s</comment> relates to (has) <info>one</info> <comment>%s</comment>", $originalEntityShort, $targetEntityShort, $targetEntityShort, $originalEntityShort),
sprintf("Each <comment>%s</comment> can relate to (can have) <info>many</info> <comment>%s</comment> objects.\nEach <comment>%s</comment> relates to (has) <info>one</info> <comment>%s</comment>.", $originalEntityShort, $targetEntityShort, $targetEntityShort, $originalEntityShort),
];
$rows[] = ['', ''];
$rows[] = [
EntityRelation::MANY_TO_MANY,
sprintf("Each <comment>%s</comment> can relate to (can have) <info>many</info> <comment>%s</comment> objects.\nEach <comment>%s</comment> can also relate to (can also have) <info>many</info> <comment>%s</comment> objects", $originalEntityShort, $targetEntityShort, $targetEntityShort, $originalEntityShort),
sprintf("Each <comment>%s</comment> can relate to (can have) <info>many</info> <comment>%s</comment> objects.\nEach <comment>%s</comment> can also relate to (can also have) <info>many</info> <comment>%s</comment> objects.", $originalEntityShort, $targetEntityShort, $targetEntityShort, $originalEntityShort),
];
$rows[] = ['', ''];
$rows[] = [
Expand Down
24 changes: 15 additions & 9 deletions src/Maker/MakeMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Util\CliOutputHelper;
use Symfony\Bundle\MakerBundle\Util\MakerFileLinkFormatter;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
Expand All @@ -33,8 +35,10 @@ final class MakeMigration extends AbstractMaker implements ApplicationAwareMaker
{
private Application $application;

public function __construct(private string $projectDir)
{
public function __construct(
private string $projectDir,
private ?MakerFileLinkFormatter $makerFileLinkFormatter = null,
) {
}

public static function getCommandName(): string
Expand Down Expand Up @@ -115,13 +119,15 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
return;
}

$this->writeSuccessMessage($io);
$absolutePath = $this->getGeneratedMigrationFilename($migrationOutput);
$relativePath = str_replace($this->projectDir.'/', '', $absolutePath);

$migrationName = $this->getGeneratedMigrationFilename($migrationOutput);
$io->comment('<fg=blue>created</>: '.($this->makerFileLinkFormatter?->makeLinkedPath($absolutePath, $relativePath) ?? $relativePath));

$this->writeSuccessMessage($io);

$io->text([
sprintf('Next: Review the new migration <info>%s</info>', $migrationName),
'Then: Run the migration with <info>php bin/console doctrine:migrations:migrate</info>',
sprintf('Review the new migration then run it with <info>%s doctrine:migrations:migrate</info>', CliOutputHelper::getCommandPrefix()),
'See <fg=yellow>https://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html</>',
]);
}
Expand All @@ -147,12 +153,12 @@ public function configureDependencies(DependencyBuilder $dependencies)

private function getGeneratedMigrationFilename(string $migrationOutput): string
{
preg_match('#"(.*?)"#', $migrationOutput, $matches);
preg_match('#"<info>(.*?)</info>"#', $migrationOutput, $matches);

if (!isset($matches[0])) {
if (!isset($matches[1])) {
throw new \Exception('Your migration generated successfully, but an error occurred printing the summary of what occurred.');
}

return str_replace($this->projectDir.'/', '', $matches[0]);
return $matches[1];
}
}
Loading

0 comments on commit d1747f0

Please sign in to comment.