[TASK] Cleanup for 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-2013 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 * @return string Name of the repository
61 */
62 static public function translateModelNameToValidatorName($modelName) {
63 return str_replace(
64 array('\\Domain\\Model\\', '_Domain_Model_'),
65 array('\\Domain\\Validator\\', '_Domain_Validator_'),
66 $modelName
67 ) . 'Validator';
68 }
69
70 /**
71 * Translates a repository name to an appropriate model name
72 * e.g. Tx_Extbase_Domain_Repository_FooRepository to Tx_Extbase_Domain_Model_Foo
73 * or \TYPO3\CMS\Extbase\Domain\Repository\FooRepository to \TYPO3\CMS\Extbase\Domain\Model\Foo
74 *
75 * @param string $repositoryName Name of the repository to translate
76 * @return string Name of the model
77 */
78 static public function translateRepositoryNameToModelName($repositoryName) {
79 return preg_replace(
80 array('/\\\\Domain\\\\Repository/', '/_Domain_Repository_/', '/Repository$/'),
81 array('\\Domain\\Model', '_Domain_Model_', ''),
82 $repositoryName
83 );
84 }
85
86
87
88 /**
89 * Explodes a controllerObjectName like \Vendor\Ext\Controller\FooController
90 * into several pieces like vendorName, extensionName, subpackageKey and controllerName
91 *
92 * @param string $controllerObjectName The controller name to be exploded
93 * @return array An array of controllerObjectName pieces
94 */
95 static public function explodeObjectControllerName($controllerObjectName) {
96 $matches = array();
97
98 if (strpos($controllerObjectName, '\\') !== FALSE) {
99 if (substr($controllerObjectName, 0, 9) === 'TYPO3\\CMS') {
100 $extensionName = '^(?P<vendorName>[^\\\\]+\\\[^\\\\]+)\\\(?P<extensionName>[^\\\\]+)';
101 } else {
102 $extensionName = '^(?P<vendorName>[^\\\\]+)\\\\(?P<extensionName>[^\\\\]+)';
103 }
104
105 preg_match(
106 '/' . $extensionName . '\\\\(Controller|Command|(?P<subpackageKey>.+)\\\\Controller)\\\\(?P<controllerName>[a-z\\\\]+)Controller$/ix',
107 $controllerObjectName,
108 $matches
109 );
110 } else {
111 preg_match(
112 '/^Tx_(?P<extensionName>[^_]+)_(Controller|Command|(?P<subpackageKey>.+)_Controller)_(?P<controllerName>[a-z_]+)Controller$/ix',
113 $controllerObjectName,
114 $matches
115 );
116 }
117
118 return $matches;
119 }
120 }
121
122 ?>