Skip to content

Commit

Permalink
[TASK] Refactor some useful methods from TCA hook to ResolveUtility
Browse files Browse the repository at this point in the history
This adds a few handy class name and -instance resolving methods to the ResolveUtility. The methods were previously called from the TableConfigurationPostProcessor hook but also make sense in other contexts.
  • Loading branch information
NamelessCoder committed Feb 22, 2015
1 parent 16972b4 commit 2cf0e62
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 67 deletions.
53 changes: 10 additions & 43 deletions Classes/Backend/TableConfigurationPostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use FluidTYPO3\Flux\Form;
use FluidTYPO3\Flux\Utility\AnnotationUtility;
use FluidTYPO3\Flux\Utility\ExtensionNamingUtility;
use FluidTYPO3\Flux\Utility\ResolveUtility;
use TYPO3\CMS\Core\Database\TableConfigurationPostProcessingHookInterface;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand Down Expand Up @@ -57,30 +58,13 @@ public function processData() {
*/
protected function generateTableConfigurationForProviderForms() {
$forms = Core::getRegisteredFormsForTables();
$models = Core::getRegisteredFormsForModelObjectClasses();
$packages = array_keys(Core::getRegisteredPackagesForAutoForms());
foreach ($packages as $packageName) {
list ($vendorName, $extensionName) = explode('.', $packageName);
$namespace = $vendorName . '\\' . $extensionName . '\\Domain\\';
$extensionKey = ExtensionNamingUtility::getExtensionKey($packageName);
$folder = ExtensionManagementUtility::extPath($extensionKey, 'Classes/Domain/Form/');
$files = glob($folder . '*Form.php');
$files = FALSE === $files ? array() : $files;
foreach ($files as $fileName) {
$basename = pathinfo($fileName, PATHINFO_FILENAME);
$formClassName = $namespace . 'Form\\' . $basename;
$modelClassName = $namespace . 'Model\\' . substr($basename, 0, -4);
$fullTableName = $this->resolveTableName($modelClassName);
$models[$modelClassName] = $formClassName::create();
$models[$modelClassName]->setName($fullTableName);
$models[$modelClassName]->setExtensionName($packageName);
}
}
$packages = $this->getInstalledFluxPackages();
$models = ResolveUtility::resolveDomainFormClassInstancesFromPackages($packages);
foreach ($forms as $fullTableName => $form) {
$this->processFormForTable($fullTableName, $form);
}
foreach ($models as $modelClassName => $form) {
$fullTableName = $this->resolveTableName($modelClassName);
$fullTableName = ResolveUtility::resolveDatabaseTableName($modelClassName);
if (NULL === $form) {
$form = $this->generateFormInstanceFromClassName($modelClassName, $fullTableName);
}
Expand All @@ -89,7 +73,13 @@ protected function generateTableConfigurationForProviderForms() {
}
$this->processFormForTable($fullTableName, $form);
}
}

/**
* @return array
*/
protected function getInstalledFluxPackages() {
return array_keys(Core::getRegisteredPackagesForAutoForms());
}

/**
Expand Down Expand Up @@ -241,27 +231,4 @@ protected function buildShowItemList(Form $form) {
return implode(', ', $parts);
}

/**
* Resolve the table name for the given class name
*
* @param string $className
* @return string The table name
*/
protected function resolveTableName($className) {
$className = ltrim($className, '\\');
if (strpos($className, '\\') !== FALSE) {
$classNameParts = explode('\\', $className, 6);
// Skip vendor and product name for core classes
if (strpos($className, 'TYPO3\\CMS\\') === 0) {
$classPartsToSkip = 2;
} else {
$classPartsToSkip = 1;
}
$tableName = 'tx_' . strtolower(implode('_', array_slice($classNameParts, $classPartsToSkip)));
} else {
$tableName = strtolower($className);
}
return $tableName;
}

}
72 changes: 72 additions & 0 deletions Classes/Utility/ResolveUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Core;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
Expand All @@ -18,6 +20,53 @@
*/
class ResolveUtility {

/**
* Resolves a list (array) of class names (not instances) of
* all classes in files in the specified sub-namespace of the
* specified package name. Does not attempt to load the class.
* Does not work recursively.
*
* @param string $packageName
* @param string $subNamespace
* @return array
*/
public static function resolveClassNamesInPackageSubNamespace($packageName, $subNamespace) {
$classNames = array();
$extensionKey = ExtensionNamingUtility::getExtensionKey($packageName);
$prefix = str_replace('.', '\\', $packageName);
$suffix = TRUE === empty($subNamespace) ? '' : str_replace('/', '\\', $subNamespace) . '\\';
$folder = ExtensionManagementUtility::extPath($extensionKey, 'Classes/' . $subNamespace);
$files = GeneralUtility::getFilesInDir($folder, 'php');
if (TRUE === is_array($files)) {
foreach ($files as $file) {
$classNames[] = $prefix . '\\' . $suffix . pathinfo($file, PATHINFO_FILENAME);
}
}
return $classNames;
}

/**
* Resolves a list (array) of instances of Form implementations
* from the provided package names, or all package names if empty.
*
* @param array $packageNames
* @return Form[]
*/
public static function resolveDomainFormClassInstancesFromPackages(array $packageNames = NULL) {
$packageNames = NULL === $packageNames ? Core::getRegisteredPackagesForAutoForms() : $packageNames;
$models = (array) Core::getRegisteredFormsForModelObjectClasses();
foreach ($packageNames as $packageName) {
$classNames = self::resolveClassNamesInPackageSubNamespace($packageName, 'Domain/Form');
foreach ($classNames as $formClassName) {
$fullTableName = self::resolveDatabaseTableName($modelClassName);
$models[$modelClassName] = $formClassName::create();
$models[$modelClassName]->setName($fullTableName);
$models[$modelClassName]->setExtensionName($packageName);
}
}
return $models;
}

/**
* @param string $extensionKey
* @param string $action
Expand Down Expand Up @@ -66,6 +115,29 @@ public static function convertAllPathSegmentsToUpperCamelCase($path) {
return $path;
}

/**
* Resolve the table name for the given class name
*
* @param string $className
* @return string The table name
*/
public static function resolveDatabaseTableName($className) {
$className = ltrim($className, '\\');
if (strpos($className, '\\') !== FALSE) {
$classNameParts = explode('\\', $className, 6);
// Skip vendor and product name for core classes
if (strpos($className, 'TYPO3\\CMS\\') === 0) {
$classPartsToSkip = 2;
} else {
$classPartsToSkip = 1;
}
$tableName = 'tx_' . strtolower(implode('_', array_slice($classNameParts, $classPartsToSkip)));
} else {
$tableName = strtolower($className);
}
return $tableName;
}

/**
* @param string $extensionKey
* @param string $controllerName
Expand Down
24 changes: 0 additions & 24 deletions Tests/Unit/Backend/TableConfigurationPostProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,4 @@ public function canExtensionNameFromNameSpacedClassNameWithVendor() {
$this->assertEquals('FluidTYPO3.Flux', $extensionName);
}

/**
* @test
* @dataProvider getClassToTableTestValues
* @param string $class
* @param string $expectedTable
*/
public function testResolveTableName($class, $expectedTable) {
$instance = new TableConfigurationPostProcessor();
$result = $this->callInaccessibleMethod($instance, 'resolveTableName', $class);
$this->assertEquals($expectedTable, $result);
}

/**
* @return array
*/
public function getClassToTableTestValues() {
return array(
array('syslog', 'syslog'),
array('FluidTYPO3\\Flux\\Domain\\Model\\ObjectName', 'tx_flux_domain_model_objectname'),
array('TYPO3\\CMS\\Extbase\\Domain\\Model\\ObjectName', 'tx_extbase_domain_model_objectname'),
array('Tx_Flux_Domain_Model_ObjectName', 'tx_flux_domain_model_objectname'),
);
}

}
31 changes: 31 additions & 0 deletions Tests/Unit/Utility/ResolveUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
*/
class ResolveUtilityTest extends AbstractTestCase {

/**
* @test
*/
public function resolvesClassNamesInSubNamespaceOfPackage() {
$result = ResolveUtility::resolveClassNamesInPackageSubNamespace('FluidTYPO3.Flux', '');
$this->assertEquals(array('FluidTYPO3\\Flux\\Core', 'FluidTYPO3\\Flux\\Form'), $result);
}

/**
* @test
*/
Expand Down Expand Up @@ -85,4 +93,27 @@ public function canDetectRequestArgumentsBasedOnPluginSignature() {
$this->assertNull($result);
}

/**
* @test
* @dataProvider getClassToTableTestValues
* @param string $class
* @param string $expectedTable
*/
public function testResolveTableName($class, $expectedTable) {
$result = ResolveUtility::resolveDatabaseTableName($class);
$this->assertEquals($expectedTable, $result);
}

/**
* @return array
*/
public function getClassToTableTestValues() {
return array(
array('syslog', 'syslog'),
array('FluidTYPO3\\Flux\\Domain\\Model\\ObjectName', 'tx_flux_domain_model_objectname'),
array('TYPO3\\CMS\\Extbase\\Domain\\Model\\ObjectName', 'tx_extbase_domain_model_objectname'),
array('Tx_Flux_Domain_Model_ObjectName', 'tx_flux_domain_model_objectname'),
);
}

}

0 comments on commit 2cf0e62

Please sign in to comment.