ClassNamingUtility.php 4.23 KB
Newer Older
1
2
3
4
5
6
<?php
namespace TYPO3\CMS\Core\Utility;

/***************************************************************
 *  Copyright notice
 *
7
 *  (c) 2012-2013 Extbase Team
8
9
10
11
12
13
14
15
16
17
 *  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.
18
 *  A copy is found in the text file GPL.txt and important notices to the license
19
20
21
22
23
24
25
26
27
28
29
 *  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!
 ***************************************************************/
/**
30
 * Several functions related to naming and conversions of names
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 * 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(
48
49
			array('\\Domain\\Model', '_Domain_Model_'),
			array('\\Domain\\Repository', '_Domain_Repository_'),
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
			$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(
80
81
			array('/\\\\Domain\\\\Repository/', '/_Domain_Repository_/', '/Repository$/'),
			array('\\Domain\\Model', '_Domain_Model_', ''),
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
			$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) {
99
100
			if (substr($controllerObjectName, 0, 9) === 'TYPO3\\CMS') {
				$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
108
109
110
111
112
113
114
115
116
117
118
119
120
				$controllerObjectName,
				$matches
			);
		} else {
			preg_match(
				'/^Tx_(?P<extensionName>[^_]+)_(Controller|Command|(?P<subpackageKey>.+)_Controller)_(?P<controllerName>[a-z_]+)Controller$/ix',
				$controllerObjectName,
				$matches
			);
		}

		return $matches;
	}
}