ClassNamingUtility.php 2.9 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
 * such as translation between Repository and Model names or
 * exploding an objectControllerName into pieces
 */
22
23
24
25
26
27
28
29
30
31
32
33
34
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(
35
36
            '\\Domain\\Model',
            '\\Domain\\Repository',
37
38
39
            $modelName
        ) . 'Repository';
    }
40

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

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

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

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

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