ClassNamingUtility.php 2.9 KB
Newer Older
1
<?php
2

3
4
namespace TYPO3\CMS\Core\Utility;

5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
9
10
 * 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.
11
 *
12
13
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
16
 * The TYPO3 project - inspiring people to share!
 */
Wouter Wolters's avatar
Wouter Wolters committed
17

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

42
43
44
45
46
47
48
49
50
51
52
    /**
     * 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(
53
54
            ['/\\\\Domain\\\\Repository/', '/Repository$/'],
            ['\\Domain\\Model', ''],
55
56
57
            $repositoryName
        );
    }
58

59
60
61
62
63
64
65
66
67
    /**
     * 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)
    {
68
        $matches = [];
69

70
        if (strpos($controllerObjectName, 'TYPO3\\CMS') === 0) {
71
            $extensionName = '^(?P<vendorName>[^\\\\]+\\\[^\\\\]+)\\\(?P<extensionName>[^\\\\]+)';
72
        } else {
73
            $extensionName = '^(?P<vendorName>[^\\\\]+)\\\\(?P<extensionName>[^\\\\]+)';
74
        }
75

76
77
78
79
80
81
        preg_match(
            '/' . $extensionName . '\\\\(Controller|Command|(?P<subpackageKey>.+)\\\\Controller)\\\\(?P<controllerName>[a-z\\\\]+)Controller$/ix',
            $controllerObjectName,
            $matches
        );

82
        return array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);
83
    }
84
}