[FEATURE] Introduce ClassNamingUtility
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / ClassNamingUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Extbase Team
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Several functions related to naming and convertions of names
31 * such as translation between Repository and Model names or
32 * exploding an objectControllerName into pieces
33 *
34 * @api
35 */
36 class ClassNamingUtility {
37
38 /**
39 * Translates a model name to an appropriate repository name
40 * e.g. Tx_Extbase_Domain_Model_Foo to Tx_Extbase_Domain_Repository_FooRepository
41 * or \TYPO3\CMS\Extbase\Domain\Model\Foo to \TYPO3\CMS\Extbase\Domain\Repository\FooRepository
42 *
43 * @param string $modelName Name of the model to translate
44 * @return string Name of the repository
45 */
46 static public function translateModelNameToRepositoryName($modelName) {
47 return str_replace(
48 array('\\Domain\\Model', 'Domain_Model'),
49 array('\\Domain\\Repository', 'Domain_Repository'),
50 $modelName
51 ) . 'Repository';
52 }
53
54 /**
55 * Translates a model name to an appropriate validator name
56 * e.g. Tx_Extbase_Domain_Model_Foo to Tx_Extbase_Domain_Validator_FooValidator
57 * or \TYPO3\CMS\Extbase\Domain\Model\Foo to \TYPO3\CMS\Extbase\Domain\Validator\FooValidator
58 *
59 * @param string $modelName Name of the model to translate
60 *
61 * @return string Name of the repository
62 */
63 static public function translateModelNameToValidatorName($modelName) {
64 return str_replace(
65 array('\\Domain\\Model\\', '_Domain_Model_'),
66 array('\\Domain\\Validator\\', '_Domain_Validator_'),
67 $modelName
68 ) . 'Validator';
69 }
70
71 /**
72 * Translates a repository name to an appropriate model name
73 * e.g. Tx_Extbase_Domain_Repository_FooRepository to Tx_Extbase_Domain_Model_Foo
74 * or \TYPO3\CMS\Extbase\Domain\Repository\FooRepository to \TYPO3\CMS\Extbase\Domain\Model\Foo
75 *
76 * @param string $repositoryName Name of the repository to translate
77 * @return string Name of the model
78 */
79 static public function translateRepositoryNameToModelName($repositoryName) {
80 return preg_replace(
81 array('/\\\Domain\\\Repository/', '/Domain_Repository/', '/Repository$/'),
82 array('\\Domain\\Model', 'Domain_Model', ''),
83 $repositoryName
84 );
85 }
86
87
88
89 /**
90 * Explodes a controllerObjectName like \Vendor\Ext\Controller\FooController
91 * into several pieces like vendorName, extensionName, subpackageKey and controllerName
92 *
93 * @param string $controllerObjectName The controller name to be exploded
94 * @return array An array of controllerObjectName pieces
95 */
96 static public function explodeObjectControllerName($controllerObjectName) {
97 $matches = array();
98
99 if (strpos($controllerObjectName, '\\') !== FALSE) {
100 if (substr($controllerObjectName, 0, 9) === 'TYPO3\CMS') {
101 $extensionName = '^(?P<vendorName>[^\\\]+\\\[^\\\]+)\\\(?P<extensionName>[^\\\]+)';
102 } else {
103 $extensionName = '^(?P<vendorName>[^\\\]+)\\\(?P<extensionName>[^\\\]+)';
104 }
105
106 preg_match(
107 '/' . $extensionName . '\\\(Controller|Command|(?P<subpackageKey>.+)\\\Controller)\\\(?P<controllerName>[a-z\\\]+)Controller$/ix',
108 $controllerObjectName,
109 $matches
110 );
111 } else {
112 preg_match(
113 '/^Tx_(?P<extensionName>[^_]+)_(Controller|Command|(?P<subpackageKey>.+)_Controller)_(?P<controllerName>[a-z_]+)Controller$/ix',
114 $controllerObjectName,
115 $matches
116 );
117 }
118
119 return $matches;
120 }
121 }
122
123 ?>