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

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

16
17
namespace TYPO3\CMS\Core\Utility;

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
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
     */
30
    public static function translateModelNameToRepositoryName(string $modelName): string
31
32
    {
        return str_replace(
33
34
            '\\Domain\\Model',
            '\\Domain\\Repository',
35
36
37
            $modelName
        ) . 'Repository';
    }
38

39
40
41
42
43
    /**
     * 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
     */
44
    public static function translateRepositoryNameToModelName(string $repositoryName): string
45
46
    {
        return preg_replace(
47
48
            ['/\\\\Domain\\\\Repository/', '/Repository$/'],
            ['\\Domain\\Model', ''],
49
50
51
            $repositoryName
        );
    }
52

53
54
55
56
57
    /**
     * 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
58
     * @return array<string> An array of controllerObjectName pieces
59
     */
60
    public static function explodeObjectControllerName(string $controllerObjectName): array
61
    {
62
        $matches = [];
63
64
65
        $extensionName = str_starts_with($controllerObjectName, 'TYPO3\\CMS')
            ? '^(?P<vendorName>[^\\\\]+\\\[^\\\\]+)\\\(?P<extensionName>[^\\\\]+)'
            : '^(?P<vendorName>[^\\\\]+)\\\\(?P<extensionName>[^\\\\]+)';
66
67
68
69
70
        preg_match(
            '/' . $extensionName . '\\\\(Controller|Command|(?P<subpackageKey>.+)\\\\Controller)\\\\(?P<controllerName>[a-z\\\\]+)Controller$/ix',
            $controllerObjectName,
            $matches
        );
71
        return array_filter($matches, is_string(...), ARRAY_FILTER_USE_KEY);
72
    }
73
}