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

[TASK] Remove code duplication of command #653

Merged
merged 3 commits into from
Dec 27, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 23 additions & 14 deletions Classes/Composer/InstallerScript/PopulateCommandConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,17 @@ public function run(ScriptEvent $event): bool
foreach ($this->extractPackageMapFromComposer($composer) as $item) {
/** @var \Composer\Package\PackageInterface $package */
list($package, $installPath) = $item;
$installPath = ($installPath ?: $basePath);
$packageName = $package->getName();
if ($package->getType() === 'metapackage') {
// We have a meta package, which does not have any files
continue;
}
$installPath = ($installPath ?: $basePath);
$packageName = $package->getName();
if ($packageName === 'typo3/cms') {
foreach (glob($installPath . '/typo3/sysext/*/') as $installPath) {
$packageName = basename($installPath);
$extensionConfig = $this->getConfigFromPackage($installPath);
if (!empty($extensionConfig)) {
$commandConfiguration[$packageName] = $extensionConfig;
}
}
}
$packageConfig = $this->getConfigFromPackage($installPath);
if (!empty($packageConfig)) {
$commandConfiguration[$packageName] = $packageConfig;
$commandConfiguration = array_merge($commandConfiguration, $this->getConfigFromTypo3Packages($installPath));
continue;
}
$commandConfiguration = array_merge($commandConfiguration, $this->getConfigFromPackage($installPath, $packageName));
}
$success = file_put_contents(
__DIR__ . '/../../../Configuration/Console/AllCommands.php',
Expand Down Expand Up @@ -86,7 +78,7 @@ private function extractPackageMapFromComposer(\Composer\Composer $composer)
* @param $installPath
* @return mixed
*/
private function getConfigFromPackage($installPath)
private function getConfigFromPackage(string $installPath, string $packageName)
{
$commandConfiguration = [];
if (file_exists($commandConfigurationFile = $installPath . '/Configuration/Console/Commands.php')) {
Expand All @@ -95,6 +87,23 @@ private function getConfigFromPackage($installPath)
if (file_exists($commandConfigurationFile = $installPath . '/Configuration/Commands.php')) {
$commandConfiguration['commands'] = require $commandConfigurationFile;
}
if (empty($commandConfiguration)) {
return [];
}
return [$packageName => $commandConfiguration];
}

/**
* @param $typo3InstallPath
* @return array
*/
private function getConfigFromTypo3Packages(string $typo3InstallPath): array
{
$commandConfiguration = [];
foreach (glob($typo3InstallPath . '/typo3/sysext/*/') as $installPath) {
$packageName = basename($installPath);
$commandConfiguration = array_merge($commandConfiguration, $this->getConfigFromPackage($installPath, $packageName));
}
return $commandConfiguration;
}
}
4 changes: 1 addition & 3 deletions Classes/Core/Booting/CompatibilityScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Helhum\Typo3Console\Extension\ExtensionConfiguration;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration as CoreExtensionConfiguration;
use TYPO3\CMS\Core\Core\Bootstrap;

class CompatibilityScripts
{
Expand All @@ -25,10 +24,9 @@ public static function initializeConfigurationManagement()
}

/**
* @param Bootstrap $bootstrap
* @deprecated can be removed when TYPO3 8 support is removed
*/
public static function initializeDatabaseConnection(Bootstrap $bootstrap)
public static function initializeDatabaseConnection()
{
// noop for TYPO3 9
}
Expand Down
5 changes: 1 addition & 4 deletions Classes/Core/Booting/Scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,7 @@ public static function initializeExtensionConfiguration(Bootstrap $bootstrap)
$bootstrap->unsetReservedGlobalVariables();
}

/**
* @param Bootstrap $bootstrap
*/
public static function initializePersistence(Bootstrap $bootstrap)
public static function initializePersistence()
{
ExtensionManagementUtility::loadBaseTca();
}
Expand Down
56 changes: 22 additions & 34 deletions Classes/Mvc/Cli/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Reflection\ClassSchema;
use TYPO3\CMS\Extbase\Reflection\ReflectionService;

/**
* Represents a Command
Expand All @@ -40,6 +38,11 @@ class Command
*/
protected $controllerCommandName;

/**
* @var string
*/
private $controllerCommandMethod;

/**
* @var string
*/
Expand All @@ -52,11 +55,6 @@ class Command
*/
protected $extensionName;

/**
* @var ReflectionService
*/
protected $reflectionService;

/**
* @var CommandArgumentDefinition[]
*/
Expand Down Expand Up @@ -88,27 +86,22 @@ class Command
private $inputDefinitions;

/**
* @var ClassSchema
* @var CommandReflection
*/
private $classSchema;

/**
* @var string
*/
private $controllerCommandMethod;
private $commandReflection;

/**
* @param string $controllerClassName Class name of the controller providing the command
* @param string $controllerCommandName Command name, i.e. the method name of the command, without the "Command" suffix
* @param ReflectionService $reflectionService
* @param CommandReflection $commandReflection
* @throws InvalidArgumentException
*/
public function __construct(string $controllerClassName, string $controllerCommandName, ReflectionService $reflectionService)
public function __construct(string $controllerClassName, string $controllerCommandName, CommandReflection $commandReflection = null)
{
$this->controllerClassName = $controllerClassName;
$this->controllerCommandName = $controllerCommandName;
$this->reflectionService = $reflectionService;
$this->controllerCommandMethod = $this->controllerCommandName . 'Command';
$this->commandReflection = $commandReflection ?: new CommandReflection($this->controllerClassName, $this->controllerCommandMethod);
$delimiter = strpos($controllerClassName, '\\') !== false ? '\\' : '_';
$classNameParts = explode($delimiter, $controllerClassName);
if (isset($classNameParts[0], $classNameParts[1]) && $classNameParts[0] === 'TYPO3' && $classNameParts[1] === 'CMS') {
Expand All @@ -134,11 +127,6 @@ public function __construct(string $controllerClassName, string $controllerComma
$this->commandIdentifier = strtolower($extensionKey . ':' . substr($classNameParts[$numberOfClassNameParts - 1], 0, -17) . ':' . $controllerCommandName);
}

public function initializeObject()
{
$this->classSchema = $this->reflectionService->getClassSchema($this->controllerClassName);
}

/**
* @return string
*/
Expand Down Expand Up @@ -182,7 +170,7 @@ public function getExtensionName(): string
*/
public function getShortDescription(): string
{
$lines = explode(LF, $this->classSchema->getMethod($this->controllerCommandMethod)['description']);
$lines = explode(LF, $this->commandReflection->getDescription());
return !empty($lines) ? trim($lines[0]) : '<no description available>';
}

Expand All @@ -195,7 +183,7 @@ public function getShortDescription(): string
*/
public function getDescription(): string
{
$lines = explode(LF, $this->classSchema->getMethod($this->controllerCommandMethod)['description']);
$lines = explode(LF, $this->commandReflection->getDescription());
array_shift($lines);
$descriptionLines = [];
foreach ($lines as $line) {
Expand All @@ -214,7 +202,7 @@ public function getDescription(): string
*/
public function hasArguments(): bool
{
return !empty($this->classSchema->getMethod($this->controllerCommandMethod)['params']);
return !empty($this->commandReflection->getParameters());
}

/**
Expand Down Expand Up @@ -280,8 +268,8 @@ public function getArgumentDefinitions(): array
return $this->argumentDefinitions = [];
}
$this->argumentDefinitions = [];
$commandParameters = $this->classSchema->getMethod($this->controllerCommandMethod)['params'];
$commandParameterTags = $this->classSchema->getMethod($this->controllerCommandMethod)['tags']['param'];
$commandParameters = $this->commandReflection->getParameters();
$commandParameterTags = $this->commandReflection->getTagsValues()['param'];
$i = 0;
$argumentNames = $this->getDefinedArgumentNames();
foreach ($commandParameters as $commandParameterName => $commandParameterDefinition) {
Expand Down Expand Up @@ -409,7 +397,7 @@ private function getCommandMethodDefinitions(): array
}

/**
* Very simple parsing of a definition annotations on command methods.
* Get parsed annotations if command has any
*
* @throws InvalidArgumentException
* @return array
Expand Down Expand Up @@ -474,7 +462,7 @@ public function getSynopsis($short = false): string
*/
public function isInternal(): bool
{
return isset($this->classSchema->getMethod($this->controllerCommandMethod)['tags']['internal']);
return isset($this->commandReflection->getTagsValues()['internal']);
}

/**
Expand All @@ -484,7 +472,7 @@ public function isInternal(): bool
*/
public function isCliOnly(): bool
{
return isset($this->classSchema->getMethod($this->controllerCommandMethod)['tags']['cli']);
return isset($this->commandReflection->getTagsValues()['cli']);
}

/**
Expand All @@ -496,7 +484,7 @@ public function isCliOnly(): bool
*/
public function isFlushingCaches(): bool
{
return isset($this->classSchema->getMethod($this->controllerCommandMethod)['tags']['flushesCaches']);
return isset($this->commandReflection->getTagsValues()['flushesCaches']);
}

/**
Expand All @@ -508,12 +496,12 @@ public function isFlushingCaches(): bool
*/
public function getRelatedCommandIdentifiers(): array
{
if (!isset($this->classSchema->getMethod($this->controllerCommandMethod)['tags']['see'])) {
if (!isset($this->commandReflection->getTagsValues()['see'])) {
return [];
}
$relatedCommandIdentifiers = [];
foreach ($this->classSchema->getMethod($this->controllerCommandMethod)['tags']['see'] as $tagValue) {
if (preg_match('/^[\\w\\d\\.]+:[\\w\\d]+:[\\w\\d]+$/', $tagValue) === 1) {
foreach ($this->commandReflection->getTagsValues()['see'] as $tagValue) {
if (preg_match('/^[\\w\\._]+:[\\w]+:[\\w]+$/', $tagValue) === 1) {
$relatedCommandIdentifiers[] = $tagValue;
}
}
Expand Down
51 changes: 51 additions & 0 deletions Classes/Mvc/Cli/CommandReflection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace Helhum\Typo3Console\Mvc\Cli;

/*
* This file is part of the TYPO3 Console project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read
* LICENSE file that was distributed with this source code.
*
*/

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Reflection\ReflectionService;

/**
* Analyze command method arguments and tags with PHP reflection
*/
class CommandReflection
{
/**
* @var array
*/
private $methodProperties;

public function __construct(string $controllerClassName, string $controllerCommandMethod, ReflectionService $reflectionService = null)
{
$reflectionService = $reflectionService ?: GeneralUtility::makeInstance(ObjectManager::class)->get(ReflectionService::class);
$this->methodProperties = $reflectionService->getClassSchema($controllerClassName)->getMethod($controllerCommandMethod);
}

public function getDescription(): string
{
return $this->methodProperties['description'] ?? '';
}

public function getParameters(): array
{
return $this->methodProperties['params'] ?? [];
}

public function getTagsValues(): array
{
return $this->methodProperties['tags'] ?? [];
}
}
2 changes: 1 addition & 1 deletion Classes/Service/CacheLowLevelCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function forceFlushDatabaseCacheTables()
// Get all table names from Default connection starting with 'cf_' and truncate them
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$connection = $connectionPool->getConnectionByName('Default');
$tablesNames = $tableNames = $connection->getSchemaManager()->listTableNames();
$tablesNames = $connection->getSchemaManager()->listTableNames();
foreach ($tablesNames as $tableName) {
if ($tableName === 'cache_treelist' || strpos($tableName, 'cf_') === 0) {
$connection->truncate($tableName);
Expand Down