ClassNamingUtility.php 3.99 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!
 */
Wouter Wolters's avatar
Wouter Wolters committed
16

17
/**
18
 * Several functions related to naming and conversions of names
19
20
21
22
23
 * such as translation between Repository and Model names or
 * exploding an objectControllerName into pieces
 *
 * @api
 */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
     */
    public static function translateModelNameToRepositoryName($modelName)
    {
        return str_replace(
            array('\\Domain\\Model', '_Domain_Model_'),
            array('\\Domain\\Repository', '_Domain_Repository_'),
            $modelName
        ) . 'Repository';
    }
42

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    /**
     * 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
     */
    public static function translateModelNameToValidatorName($modelName)
    {
        return str_replace(
            array('\\Domain\\Model\\', '_Domain_Model_'),
            array('\\Domain\\Validator\\', '_Domain_Validator_'),
            $modelName
        ) . 'Validator';
    }
59

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    /**
     * 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
     */
    public static function translateRepositoryNameToModelName($repositoryName)
    {
        return preg_replace(
            array('/\\\\Domain\\\\Repository/', '/_Domain_Repository_/', '/Repository$/'),
            array('\\Domain\\Model', '_Domain_Model_', ''),
            $repositoryName
        );
    }
76

77
78
79
80
81
82
83
84
85
86
    /**
     * 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
     */
    public static function explodeObjectControllerName($controllerObjectName)
    {
        $matches = array();
87

88
89
90
91
92
93
        if (strpos($controllerObjectName, '\\') !== false) {
            if (substr($controllerObjectName, 0, 9) === 'TYPO3\\CMS') {
                $extensionName = '^(?P<vendorName>[^\\\\]+\\\[^\\\\]+)\\\(?P<extensionName>[^\\\\]+)';
            } else {
                $extensionName = '^(?P<vendorName>[^\\\\]+)\\\\(?P<extensionName>[^\\\\]+)';
            }
94

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

108
109
        return $matches;
    }
110
}