Skip to content
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"symfony/config": "^3.4|^4.0|^5.0",
"symfony/console": "^3.4|^4.0|^5.0",
"symfony/dependency-injection": "^3.4|^4.0|^5.0",
"symfony/deprecation-contracts": "^2.3",
"symfony/filesystem": "^3.4|^4.0|^5.0",
"symfony/finder": "^3.4|^4.0|^5.0",
"symfony/framework-bundle": "^3.4|^4.0|^5.0",
Expand Down
13 changes: 12 additions & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;

/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
Expand All @@ -25,12 +26,21 @@ class Generator
private $twigHelper;
private $pendingOperations = [];
private $namespacePrefix;
private $phpCompatUtil;

public function __construct(FileManager $fileManager, string $namespacePrefix)
public function __construct(FileManager $fileManager, string $namespacePrefix, PhpCompatUtil $phpCompatUtil = null)
{
$this->fileManager = $fileManager;
$this->twigHelper = new GeneratorTwigHelper($fileManager);
$this->namespacePrefix = trim($namespacePrefix, '\\');

if (null === $phpCompatUtil) {
$phpCompatUtil = new PhpCompatUtil($fileManager);

trigger_deprecation('symfony/maker-bundle', '1.25', 'Initializing Generator without providing an instance of PhpCompatUtil is deprecated.');
}

$this->phpCompatUtil = $phpCompatUtil;
}

/**
Expand Down Expand Up @@ -157,6 +167,7 @@ private function addOperation(string $targetPath, string $templateName, array $v
}

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

$templatePath = $templateName;
if (!file_exists($templatePath)) {
Expand Down
5 changes: 0 additions & 5 deletions src/Maker/AbstractMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,4 @@ protected function addDependencies(array $dependencies, string $message = null):
$message
);
}

final protected function useAttributes(): bool
{
return \PHP_VERSION_ID >= 80000;
}
}
1 change: 0 additions & 1 deletion src/Maker/MakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$controllerClassNameDetails->getFullName(),
'controller/Controller.tpl.php',
[
'use_attributes' => $this->useAttributes(),
'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'with_template' => $this->isTwigInstalled() && !$noTemplate,
Expand Down
1 change: 0 additions & 1 deletion src/Maker/MakeCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$controllerClassDetails->getFullName(),
'crud/controller/Controller.tpl.php',
array_merge([
'use_attributes' => $this->useAttributes(),
'entity_full_class_name' => $entityClassDetails->getFullName(),
'entity_class_name' => $entityClassDetails->getShortName(),
'form_full_class_name' => $formClassDetails->getFullName(),
Expand Down
1 change: 0 additions & 1 deletion src/Maker/MakeRegistrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$controllerClassNameDetails->getFullName(),
'registration/RegistrationController.tpl.php',
[
'use_attributes' => $this->useAttributes(),
'route_path' => '/register',
'route_name' => 'app_register',
'form_class_name' => $formClassDetails->getShortName(),
Expand Down
1 change: 0 additions & 1 deletion src/Maker/MakeResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$controllerClassNameDetails->getFullName(),
'resetPassword/ResetPasswordController.tpl.php',
[
'use_attributes' => $this->useAttributes(),
'user_full_class_name' => $userClassNameDetails->getFullName(),
'user_class_name' => $userClassNameDetails->getShortName(),
'request_form_type_full_class_name' => $requestFormTypeClassNameDetails->getFullName(),
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<service id="maker.generator" class="Symfony\Bundle\MakerBundle\Generator">
<argument type="service" id="maker.file_manager" />
<argument /> <!-- root namespace -->
<argument type="service" id="maker.php_compat_util" />
</service>

<service id="maker.entity_class_generator" class="Symfony\Bundle\MakerBundle\Doctrine\EntityClassGenerator">
Expand All @@ -63,5 +64,9 @@
<service id="maker.renderer.form_type_renderer" class="Symfony\Bundle\MakerBundle\Renderer\FormTypeRenderer">
<argument type="service" id="maker.generator" />
</service>

<service id="maker.php_compat_util" class="Symfony\Bundle\MakerBundle\Util\PhpCompatUtil">
<argument type="service" id="maker.file_manager" />
</service>
</services>
</container>
56 changes: 56 additions & 0 deletions src/Util/PhpCompatUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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\Util;

use Symfony\Bundle\MakerBundle\FileManager;

/**
* @author Jesse Rushlow <jr@rushlow.dev>
*
* @internal
*/
class PhpCompatUtil
{
/** @var FileManager */
private $fileManager;

public function __construct(FileManager $fileManager)
{
$this->fileManager = $fileManager;
}

public function canUseAttributes(): bool
{
$version = $this->getPhpVersion();

return version_compare($version, '8alpha', '>=');
}

protected function getPhpVersion(): string
{
$rootDirectory = $this->fileManager->getRootDirectory();

$composerLockPath = sprintf('%s/composer.lock', $rootDirectory);

if (!$this->fileManager->fileExists($composerLockPath)) {
return PHP_VERSION;
}

$lockFileContents = json_decode($this->fileManager->getFileContents($composerLockPath), true);

if (empty($lockFileContents['platform-overrides']) || empty($lockFileContents['platform-overrides']['php'])) {
return PHP_VERSION;
}

return $lockFileContents['platform-overrides']['php'];
}
}
9 changes: 7 additions & 2 deletions tests/Command/MakerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Bundle\MakerBundle\FileManager;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\MakerInterface;
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;
use Symfony\Component\Console\Tester\CommandTester;

class MakerCommandTest extends TestCase
Expand All @@ -35,7 +36,9 @@ public function testExceptionOnMissingDependencies()

$fileManager = $this->createMock(FileManager::class);

$command = new MakerCommand($maker, $fileManager, new Generator($fileManager, 'App'));
$mockPhpCompatUtil = $this->createMock(PhpCompatUtil::class);

$command = new MakerCommand($maker, $fileManager, new Generator($fileManager, 'App', $mockPhpCompatUtil));
// needed because it's normally set by the Application
$command->setName('make:foo');
$tester = new CommandTester($command);
Expand All @@ -48,7 +51,9 @@ public function testExceptionOnUnknownRootNamespace()

$fileManager = $this->createMock(FileManager::class);

$command = new MakerCommand($maker, $fileManager, new Generator($fileManager, 'Unknown'));
$mockPhpCompatUtil = $this->createMock(PhpCompatUtil::class);

$command = new MakerCommand($maker, $fileManager, new Generator($fileManager, 'Unknown', $mockPhpCompatUtil));
// needed because it's normally set by the Application
$command->setName('make:foo');
$tester = new CommandTester($command);
Expand Down
4 changes: 3 additions & 1 deletion tests/Doctrine/EntityRegeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\Util\AutoloaderUtil;
use Symfony\Bundle\MakerBundle\Util\MakerFileLinkFormatter;
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Filesystem\Filesystem;
Expand Down Expand Up @@ -113,7 +114,8 @@ private function doTestRegeneration(string $sourceDir, Kernel $kernel, string $n

$fileManager = new FileManager($fs, $autoloaderUtil, new MakerFileLinkFormatter(null), $tmpDir);
$doctrineHelper = new DoctrineHelper('App\\Entity', $container->get('doctrine'));
$generator = new Generator($fileManager, 'App\\');
$phpCompatUtil = new PhpCompatUtil($fileManager);
$generator = new Generator($fileManager, 'App\\', $phpCompatUtil);
$entityClassGenerator = new EntityClassGenerator($generator, $doctrineHelper);
$entityClassGenerator->setMangerRegistryClassName(ManagerRegistry::class);
$regenerator = new EntityRegenerator(
Expand Down
7 changes: 6 additions & 1 deletion tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\MakerBundle\FileManager;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;

class GeneratorTest extends TestCase
{
Expand All @@ -26,7 +27,11 @@ public function testCreateClassNameDetails(string $name, string $prefix, string
$fileManager->expects($this->any())
->method('getNamespacePrefixForClass')
->willReturn('Foo');
$generator = new Generator($fileManager, 'App\\');

$mockPhpCompatUtil = $this->createMock(PhpCompatUtil::class);

$generator = new Generator($fileManager, 'App\\', $mockPhpCompatUtil);

$classNameDetails = $generator->createClassNameDetails($name, $prefix, $suffix);

$this->assertSame($expectedFullClassName, $classNameDetails->getFullName());
Expand Down
140 changes: 140 additions & 0 deletions tests/Util/PhpVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?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\Tests\Util;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\MakerBundle\FileManager;
use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil;

/**
* @author Jesse Rushlow <jr@rushlow.dev>
*/
class PhpVersionTest extends TestCase
{
/**
* @dataProvider phpVersionDataProvider
*/
public function testUsesPhpPlatformFromComposerJsonFile(string $version, bool $expectedResult): void
{
$json = sprintf('{"platform-overrides": {"php": "%s"}}', $version);

$mockFileManager = $this->createMock(FileManager::class);
$mockFileManager
->expects(self::once())
->method('getRootDirectory')
->willReturn('/test')
;

$mockFileManager
->expects(self::once())
->method('fileExists')
->with('/test/composer.lock')
->willReturn(true)
;

$mockFileManager
->expects(self::once())
->method('getFileContents')
->with('/test/composer.lock')
->willReturn($json)
;

$version = new PhpCompatUtil($mockFileManager);

$result = $version->canUseAttributes();

self::assertSame($expectedResult, $result);
}

public function phpVersionDataProvider(): \Generator
{
yield ['8', true];
yield ['8.0', true];
yield ['8.0.1', true];
yield ['8RC1', true];
yield ['8.1alpha1', true];
yield ['8.0.0beta2', true];
yield ['8beta', true];
yield ['7', false];
yield ['7.0', false];
yield ['7.1.1', false];
yield ['7.0.0RC3', false];
}

public function testFallBackToPhpVersionWithoutLockFile(): void
{
$mockFileManager = $this->createMock(FileManager::class);
$mockFileManager
->expects(self::once())
->method('getRootDirectory')
->willReturn('/test')
;

$mockFileManager
->expects(self::once())
->method('fileExists')
->with('/test/composer.lock')
->willReturn(false)
;

$mockFileManager
->expects(self::never())
->method('getFileContents')
;

$util = new PhpCompatUtilTestFixture($mockFileManager);

$result = $util->getVersionForTest();

self::assertSame(PHP_VERSION, $result);
}

public function testWithoutPlatformVersionSet(): void
{
$json = '{"platform-overrides": {}}';

$mockFileManager = $this->createMock(FileManager::class);
$mockFileManager
->expects(self::once())
->method('getRootDirectory')
->willReturn('/test')
;

$mockFileManager
->expects(self::once())
->method('fileExists')
->with('/test/composer.lock')
->willReturn(true)
;

$mockFileManager
->expects(self::once())
->method('getFileContents')
->with('/test/composer.lock')
->willReturn($json)
;

$util = new PhpCompatUtilTestFixture($mockFileManager);

$result = $util->getVersionForTest();

self::assertSame(PHP_VERSION, $result);
}
}

class PhpCompatUtilTestFixture extends PhpCompatUtil
{
public function getVersionForTest(): string
{
return $this->getPhpVersion();
}
}