[FEATURE] Introduce ClassNamingUtility 18/16518/4
authorAnja Leichsenring <aleichsenring@ab-softlab.de>
Sat, 17 Nov 2012 11:51:50 +0000 (12:51 +0100)
committerAndreas Wolf <andreas.wolf@typo3.org>
Sun, 10 Feb 2013 13:52:57 +0000 (14:52 +0100)
Introduce a utility class for several naming issues like
converting class names from Model to Repository and
vice versa and exploding controllerObjectNames into used parts.

Thanks to Alexander Schnitzler for the initial patch.

Releases: 6.1
Fixes: #43052
Change-Id: I2e056498cb7ce4e193f71d7c3fea908418f5de7c
Reviewed-on: https://review.typo3.org/16518
Reviewed-by: Markus Günther
Tested-by: Markus Günther
Reviewed-by: Andreas Wolf
Tested-by: Andreas Wolf
typo3/sysext/core/Classes/Utility/ClassNamingUtility.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Utility/ClassNamingUtilityTest.php [new file with mode: 0644]

diff --git a/typo3/sysext/core/Classes/Utility/ClassNamingUtility.php b/typo3/sysext/core/Classes/Utility/ClassNamingUtility.php
new file mode 100644 (file)
index 0000000..6712eb4
--- /dev/null
@@ -0,0 +1,123 @@
+<?php
+namespace TYPO3\CMS\Core\Utility;
+
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2012 Extbase Team
+ *  All rights reserved
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *  A copy is found in the textfile GPL.txt and important notices to the license
+ *  from the author is found in LICENSE.txt distributed with these scripts.
+ *
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+/**
+ * Several functions related to naming and convertions of names
+ * such as translation between Repository and Model names or
+ * exploding an objectControllerName into pieces
+ *
+ * @api
+ */
+class ClassNamingUtility {
+
+       /**
+        * Translates a model name to an appropriate repository name
+        * e.g. Tx_Extbase_Domain_Model_Foo to Tx_Extbase_Domain_Repository_FooRepository
+        * or \TYPO3\CMS\Extbase\Domain\Model\Foo to \TYPO3\CMS\Extbase\Domain\Repository\FooRepository
+        *
+        * @param string $modelName Name of the model to translate
+        * @return string Name of the repository
+        */
+       static public function translateModelNameToRepositoryName($modelName) {
+               return str_replace(
+                       array('\\Domain\\Model', 'Domain_Model'),
+                       array('\\Domain\\Repository', 'Domain_Repository'),
+                       $modelName
+               ) . 'Repository';
+       }
+
+       /**
+        * Translates a model name to an appropriate validator name
+        * e.g. Tx_Extbase_Domain_Model_Foo to Tx_Extbase_Domain_Validator_FooValidator
+        * or \TYPO3\CMS\Extbase\Domain\Model\Foo to \TYPO3\CMS\Extbase\Domain\Validator\FooValidator
+        *
+        * @param string $modelName Name of the model to translate
+        *
+        * @return string Name of the repository
+        */
+       static public function translateModelNameToValidatorName($modelName) {
+               return str_replace(
+                       array('\\Domain\\Model\\', '_Domain_Model_'),
+                       array('\\Domain\\Validator\\', '_Domain_Validator_'),
+                       $modelName
+               ) . 'Validator';
+       }
+
+       /**
+        * Translates a repository name to an appropriate model name
+        * e.g. Tx_Extbase_Domain_Repository_FooRepository to Tx_Extbase_Domain_Model_Foo
+        * or \TYPO3\CMS\Extbase\Domain\Repository\FooRepository to \TYPO3\CMS\Extbase\Domain\Model\Foo
+        *
+        * @param string $repositoryName Name of the repository to translate
+        * @return string Name of the model
+        */
+       static public function translateRepositoryNameToModelName($repositoryName) {
+               return preg_replace(
+                       array('/\\\Domain\\\Repository/', '/Domain_Repository/', '/Repository$/'),
+                       array('\\Domain\\Model', 'Domain_Model', ''),
+                       $repositoryName
+               );
+       }
+
+
+
+       /**
+        * Explodes a controllerObjectName like \Vendor\Ext\Controller\FooController
+        * into several pieces like vendorName, extensionName, subpackageKey and controllerName
+        *
+        * @param string $controllerObjectName The controller name to be exploded
+        * @return array An array of controllerObjectName pieces
+        */
+       static public function explodeObjectControllerName($controllerObjectName) {
+               $matches = array();
+
+               if (strpos($controllerObjectName, '\\') !== FALSE) {
+                       if (substr($controllerObjectName, 0, 9) === 'TYPO3\CMS') {
+                               $extensionName = '^(?P<vendorName>[^\\\]+\\\[^\\\]+)\\\(?P<extensionName>[^\\\]+)';
+                       } else {
+                               $extensionName = '^(?P<vendorName>[^\\\]+)\\\(?P<extensionName>[^\\\]+)';
+                       }
+
+                       preg_match(
+                               '/' . $extensionName . '\\\(Controller|Command|(?P<subpackageKey>.+)\\\Controller)\\\(?P<controllerName>[a-z\\\]+)Controller$/ix',
+                               $controllerObjectName,
+                               $matches
+                       );
+               } else {
+                       preg_match(
+                               '/^Tx_(?P<extensionName>[^_]+)_(Controller|Command|(?P<subpackageKey>.+)_Controller)_(?P<controllerName>[a-z_]+)Controller$/ix',
+                               $controllerObjectName,
+                               $matches
+                       );
+               }
+
+               return $matches;
+       }
+}
+
+?>
\ No newline at end of file
diff --git a/typo3/sysext/core/Tests/Unit/Utility/ClassNamingUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/ClassNamingUtilityTest.php
new file mode 100644 (file)
index 0000000..363afbd
--- /dev/null
@@ -0,0 +1,259 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Unit\Utility;
+
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2012 Extbase Team
+ *  All rights reserved
+ *
+ *  This script is part of the TYPO3 project. The TYPO3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+/**
+ * Testcase for class \TYPO3\CMS\Extbase\Utility\NamingUtilityTest
+ */
+class ClassNamingUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+
+       /**
+        * DataProvider for translateModelNameToRepositoryName
+        * and translateRepositoryNameToModelName
+        *
+        * @return array
+        */
+       public function repositoryAndModelClassNames() {
+               return array(
+                       array(
+                               'Tx_BlogExample_Domain_Repository_BlogRepository',
+                               'Tx_BlogExample_Domain_Model_Blog',
+                               'Tx_BlogExample_Domain_Validator_BlogValidator'
+                       ),
+                       array(
+                               ' _Domain_Repository_Content_PageRepository',
+                               ' _Domain_Model_Content_Page',
+                               ' _Domain_Validator_Content_PageValidator'
+                       ),
+                       array(
+                               'Tx_RepositoryExample_Domain_Repository_SomeModelRepository',
+                               'Tx_RepositoryExample_Domain_Model_SomeModel',
+                               'Tx_RepositoryExample_Domain_Validator_SomeModelValidator'
+                       ),
+                       array(
+                               'Tx_RepositoryExample_Domain_Repository_RepositoryRepository',
+                               'Tx_RepositoryExample_Domain_Model_Repository',
+                               'Tx_RepositoryExample_Domain_Validator_RepositoryValidator'
+                       ),
+                       array(
+                               'Tx_Repository_Domain_Repository_RepositoryRepository',
+                               'Tx_Repository_Domain_Model_Repository',
+                               'Tx_Repository_Domain_Validator_RepositoryValidator'
+                       ),
+                       array(
+                               'Tx_ModelCollection_Domain_Repository_ModelRepository',
+                               'Tx_ModelCollection_Domain_Model_Model',
+                               'Tx_ModelCollection_Domain_Validator_ModelValidator'
+                       ),
+                       array(
+                               'Tx_Model_Domain_Repository_ModelRepository',
+                               'Tx_Model_Domain_Model_Model',
+                               'Tx_Model_Domain_Validator_ModelValidator'
+                       ),
+                       array(
+                               'VENDOR\\EXT\\Domain\\Repository\\BlogRepository',
+                               'VENDOR\\EXT\\Domain\\Model\\Blog',
+                               'VENDOR\\EXT\\Domain\\Validator\\BlogValidator'
+                       ),
+                       array(
+                               'VENDOR\\EXT\\Domain\\Repository\\_PageRepository',
+                               'VENDOR\\EXT\\Domain\\Model\\_Page',
+                               'VENDOR\\EXT\\Domain\\Validator\\_PageValidator'
+                       ),
+                       array(
+                               'VENDOR\\Repository\\Domain\\Repository\\SomeModelRepository',
+                               'VENDOR\\Repository\\Domain\\Model\\SomeModel',
+                               'VENDOR\\Repository\\Domain\\Validator\\SomeModelValidator'
+                       ),
+                       array(
+                               'VENDOR\\EXT\\Domain\\Repository\\RepositoryRepository',
+                               'VENDOR\\EXT\\Domain\\Model\\Repository',
+                               'VENDOR\\EXT\\Domain\\Validator\\RepositoryValidator'
+                       ),
+                       array(
+                               'VENDOR\\Repository\\Domain\\Repository\\RepositoryRepository',
+                               'VENDOR\\Repository\\Domain\\Model\\Repository',
+                               'VENDOR\\Repository\\Domain\\Validator\\RepositoryValidator'
+                       ),
+                       array(
+                               'VENDOR\\ModelCollection\\Domain\\Repository\\ModelRepository',
+                               'VENDOR\\ModelCollection\\Domain\\Model\\Model',
+                               'VENDOR\\ModelCollection\\Domain\\Validator\\ModelValidator'
+                       ),
+                       array(
+                               'VENDOR\\Model\\Domain\\Repository\\ModelRepository',
+                               'VENDOR\\Model\\Domain\\Model\\Model',
+                               'VENDOR\\Model\\Domain\\Validator\\ModelValidator'
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider repositoryAndModelClassNames
+        * @param string $expectedRepositoryName
+        * @param string $modelName
+        * @test
+        */
+       public function translateModelNameToRepositoryName($expectedRepositoryName, $modelName) {
+               $translatedRepositoryName = \TYPO3\CMS\Core\Utility\ClassNamingUtility::translateModelNameToRepositoryName($modelName);
+               $this->assertSame($expectedRepositoryName, $translatedRepositoryName);
+       }
+
+       /**
+        * @dataProvider repositoryAndModelClassNames
+        * @param string $repositoryName
+        * @param string $expectedModelName
+        * @test
+        */
+       public function translateRepositoryNameToModelName($repositoryName, $expectedModelName) {
+               $translatedModelName = \TYPO3\CMS\Core\Utility\ClassNamingUtility::translateRepositoryNameToModelName($repositoryName);
+               $this->assertSame($expectedModelName, $translatedModelName);
+       }
+
+       /**
+        * @dataProvider repositoryAndModelClassNames
+        * @param string $repositoryName
+        * @param string $modelName
+        * @param string $expectedValidatorName
+        * @test
+        */
+       public function translateModelNameToValidatorName($repositoryName, $modelName, $expectedValidatorName) {
+               $translatedModelName = \TYPO3\CMS\Core\Utility\ClassNamingUtility::translateModelNameToValidatorName($modelName);
+               $this->assertSame($expectedValidatorName, $translatedModelName);
+       }
+
+       /**
+        * DataProvider for explodeObjectControllerName
+        *
+        * @return array
+        */
+       public function controllerObjectNamesAndMatches() {
+               return array(
+                       array(
+                               'TYPO3\\CMS\\Ext\\Controller\\FooController',
+                               array(
+                                       'vendorName' => 'TYPO3\\CMS',
+                                       'extensionName' => 'Ext',
+                                       'subpackageKey' => '',
+                                       'controllerName' => 'Foo',
+                               )
+                       ),
+                       array(
+                               'TYPO3\\CMS\\Ext\\Command\\FooCommandController',
+                               array(
+                                       'vendorName' => 'TYPO3\\CMS',
+                                       'extensionName' => 'Ext',
+                                       'subpackageKey' => '',
+                                       'controllerName' => 'FooCommand',
+                               )
+                       ),
+                       array(
+                               'TYPO3\\CMS\\Fluid\\ViewHelpers\\Widget\\Controller\\PaginateController',
+                               array(
+                                       'vendorName' => 'TYPO3\\CMS',
+                                       'extensionName' => 'Fluid',
+                                       'subpackageKey' => 'ViewHelpers\\Widget',
+                                       'controllerName' => 'Paginate',
+                               )
+                       ),
+                       array(
+                               'VENDOR\\Ext\\Controller\\FooController',
+                               array(
+                                       'vendorName' => 'VENDOR',
+                                       'extensionName' => 'Ext',
+                                       'subpackageKey' => '',
+                                       'controllerName' => 'Foo',
+                               )
+                       ),
+                       array(
+                               'VENDOR\\Ext\\Command\\FooCommandController',
+                               array(
+                                       'vendorName' => 'VENDOR',
+                                       'extensionName' => 'Ext',
+                                       'subpackageKey' => '',
+                                       'controllerName' => 'FooCommand',
+                               )
+                       ),
+                       array(
+                               'VENDOR\\Ext\\ViewHelpers\\Widget\\Controller\\FooController',
+                               array(
+                                       'vendorName' => 'VENDOR',
+                                       'extensionName' => 'Ext',
+                                       'subpackageKey' => 'ViewHelpers\\Widget',
+                                       'controllerName' => 'Foo',
+                               )
+                       ),
+                       // Oldschool
+                       array(
+                               'Tx_Ext_Controller_FooController',
+                               array(
+                                       'vendorName' => NULL,
+                                       'extensionName' => 'Ext',
+                                       'subpackageKey' => '',
+                                       'controllerName' => 'Foo',
+                               )
+                       ),
+                       array(
+                               'Tx_Ext_Command_FooCommandController',
+                               array(
+                                       'vendorName' => NULL,
+                                       'extensionName' => 'Ext',
+                                       'subpackageKey' => '',
+                                       'controllerName' => 'FooCommand',
+                               )
+                       ),
+                       array(
+                               'Tx_Fluid_ViewHelpers_Widget_Controller_PaginateController',
+                               array(
+                                       'vendorName' => NULL,
+                                       'extensionName' => 'Fluid',
+                                       'subpackageKey' => 'ViewHelpers_Widget',
+                                       'controllerName' => 'Paginate',
+                               )
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider controllerObjectNamesAndMatches
+        *
+        * @param string $controllerObjectName
+        * @param array $expectedMatches
+        * @test
+        */
+       public function explodeObjectControllerName($controllerObjectName, $expectedMatches) {
+               $matches = \TYPO3\CMS\Core\Utility\ClassNamingUtility::explodeObjectControllerName($controllerObjectName);
+
+               $actualMatches = array(
+                       'vendorName' => $matches['vendorName'],
+                       'extensionName' => $matches['extensionName'],
+                       'subpackageKey' => $matches['subpackageKey'],
+                       'controllerName' => $matches['controllerName'],
+               );
+
+               $this->assertSame($expectedMatches, $actualMatches);
+       }
+}
+
+?>
\ No newline at end of file