ClassNamingUtility.php 3.57 KB
Newer Older
1
2
3
<?php
namespace TYPO3\CMS\Core\Utility;

4
5
/**
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
16
/**
17
 * Several functions related to naming and conversions of names
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 * 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(
35
36
			array('\\Domain\\Model', '_Domain_Model_'),
			array('\\Domain\\Repository', '_Domain_Repository_'),
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
			$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(
67
68
			array('/\\\\Domain\\\\Repository/', '/_Domain_Repository_/', '/Repository$/'),
			array('\\Domain\\Model', '_Domain_Model_', ''),
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
			$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) {
86
87
			if (substr($controllerObjectName, 0, 9) === 'TYPO3\\CMS') {
				$extensionName = '^(?P<vendorName>[^\\\\]+\\\[^\\\\]+)\\\(?P<extensionName>[^\\\\]+)';
88
			} else {
89
				$extensionName = '^(?P<vendorName>[^\\\\]+)\\\\(?P<extensionName>[^\\\\]+)';
90
91
92
			}

			preg_match(
93
				'/' . $extensionName . '\\\\(Controller|Command|(?P<subpackageKey>.+)\\\\Controller)\\\\(?P<controllerName>[a-z\\\\]+)Controller$/ix',
94
95
96
97
98
99
100
101
102
103
104
105
106
107
				$controllerObjectName,
				$matches
			);
		} else {
			preg_match(
				'/^Tx_(?P<extensionName>[^_]+)_(Controller|Command|(?P<subpackageKey>.+)_Controller)_(?P<controllerName>[a-z_]+)Controller$/ix',
				$controllerObjectName,
				$matches
			);
		}

		return $matches;
	}
}