Revert "[TASK] Introduce DeprecationUtility and move methods"
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / ClassNamingUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Several functions related to naming and conversions of names
19 * such as translation between Repository and Model names or
20 * exploding an objectControllerName into pieces
21 *
22 * @api
23 */
24 class ClassNamingUtility
25 {
26 /**
27 * Translates a model name to an appropriate repository name
28 * e.g. Tx_Extbase_Domain_Model_Foo to Tx_Extbase_Domain_Repository_FooRepository
29 * or \TYPO3\CMS\Extbase\Domain\Model\Foo to \TYPO3\CMS\Extbase\Domain\Repository\FooRepository
30 *
31 * @param string $modelName Name of the model to translate
32 * @return string Name of the repository
33 */
34 public static function translateModelNameToRepositoryName($modelName)
35 {
36 return str_replace(
37 array('\\Domain\\Model', '_Domain_Model_'),
38 array('\\Domain\\Repository', '_Domain_Repository_'),
39 $modelName
40 ) . 'Repository';
41 }
42
43 /**
44 * Translates a model name to an appropriate validator name
45 * e.g. Tx_Extbase_Domain_Model_Foo to Tx_Extbase_Domain_Validator_FooValidator
46 * or \TYPO3\CMS\Extbase\Domain\Model\Foo to \TYPO3\CMS\Extbase\Domain\Validator\FooValidator
47 *
48 * @param string $modelName Name of the model to translate
49 * @return string Name of the repository
50 */
51 public static function translateModelNameToValidatorName($modelName)
52 {
53 return str_replace(
54 array('\\Domain\\Model\\', '_Domain_Model_'),
55 array('\\Domain\\Validator\\', '_Domain_Validator_'),
56 $modelName
57 ) . 'Validator';
58 }
59
60 /**
61 * Translates a repository name to an appropriate model name
62 * e.g. Tx_Extbase_Domain_Repository_FooRepository to Tx_Extbase_Domain_Model_Foo
63 * or \TYPO3\CMS\Extbase\Domain\Repository\FooRepository to \TYPO3\CMS\Extbase\Domain\Model\Foo
64 *
65 * @param string $repositoryName Name of the repository to translate
66 * @return string Name of the model
67 */
68 public static function translateRepositoryNameToModelName($repositoryName)
69 {
70 return preg_replace(
71 array('/\\\\Domain\\\\Repository/', '/_Domain_Repository_/', '/Repository$/'),
72 array('\\Domain\\Model', '_Domain_Model_', ''),
73 $repositoryName
74 );
75 }
76
77 /**
78 * Explodes a controllerObjectName like \Vendor\Ext\Controller\FooController
79 * into several pieces like vendorName, extensionName, subpackageKey and controllerName
80 *
81 * @param string $controllerObjectName The controller name to be exploded
82 * @return array An array of controllerObjectName pieces
83 */
84 public static function explodeObjectControllerName($controllerObjectName)
85 {
86 $matches = array();
87
88 if (strpos($controllerObjectName, '\\') !== false) {
89 if (substr($controllerObjectName, 0, 9) === 'TYPO3\\CMS') {
90 $extensionName = '^(?P<vendorName>[^\\\\]+\\\[^\\\\]+)\\\(?P<extensionName>[^\\\\]+)';
91 } else {
92 $extensionName = '^(?P<vendorName>[^\\\\]+)\\\\(?P<extensionName>[^\\\\]+)';
93 }
94
95 preg_match(
96 '/' . $extensionName . '\\\\(Controller|Command|(?P<subpackageKey>.+)\\\\Controller)\\\\(?P<controllerName>[a-z\\\\]+)Controller$/ix',
97 $controllerObjectName,
98 $matches
99 );
100 } else {
101 preg_match(
102 '/^Tx_(?P<extensionName>[^_]+)_(Controller|Command|(?P<subpackageKey>.+)_Controller)_(?P<controllerName>[a-z_]+)Controller$/ix',
103 $controllerObjectName,
104 $matches
105 );
106 }
107
108 return $matches;
109 }
110 }