Completed
Push — 8.x-1.x ( 0fa5ac...7e02f0 )
by
unknown
20:18
created

TemplateResolver::resolveByControllerAndAction()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 9.0534
cc 4
eloc 13
nc 5
nop 2
crap 4
1
<?php
2
3
namespace Drupal\controller_annotations\Templating;
4
5
class TemplateResolver
6
{
7
    /**
8
     * Convert controller class
9
     * "Drupal\<module>\Controller\<controller>Controller"
10
     * and controller action
11
     * "<action>Action"
12
     * into template file path:
13
     * "modules/<module>/templates/<module>-<controller>-<action>.html.twig"
14
     *
15
     * @param string $controllerClass
16
     * @param string $action
17
     * @return string
18
     */
19 9
    public function resolveByControllerAndAction($controllerClass, $action)
20
    {
21 9
        preg_match('/^Drupal\\\(.*)\\\Controller\\\(.*)/', $controllerClass, $data);
22 9
        if (!empty($data)) {
23 8
            $module = $data[1];
24 8
            $controller = $data[2];
25
        } else {
26 1
            throw new \InvalidArgumentException(
27 1
                sprintf('Controller class "%s" not supported', $controllerClass)
28
            );
29
        }
30
31 8
        if (preg_match('/^(.+)Controller$/', $controller, $matchController)) {
32 6
            $controller = $matchController[1];
33
        }
34 8
        if (preg_match('/^(.+)Action$/', $action, $matchAction)) {
35 6
            $action = $matchAction[1];
36
        }
37
38 8
        return $this->format($module, $controller, $action);
39
    }
40
41
    /**
42
     * Convert
43
     * "<module>:<controller>"
44
     * and
45
     * "<module>:<controller>:<action>"
46
     * into
47
     * "modules/<module>/templates/<module>-<controller>(-<action>).html.twig"
48
     *
49
     * @param string $template
50
     * @return string
51
     */
52 5
    public function normalize($template)
53
    {
54 5
        if (preg_match('/^(.+):(.+):(.+)$/', $template, $matches)) {
55 2
            return $this->format($matches[1], $matches[2], $matches[3]);
56
        }
57 4
        if (preg_match('/^(.+):(.+)$/', $template, $matches)) {
58 3
            return $this->format($matches[1], $matches[2]);
59
        }
60
61 1
        throw new \InvalidArgumentException(
62 1
            sprintf('Template pattern "%s" not supported', $template)
63
        );
64
    }
65
66
    /**
67
     * @param string $module
68
     * @param string $controller
69
     * @param string $action
70
     * @return string
71
     */
72 11
    private function format($module, $controller, $action = null)
73
    {
74 11
        $controller = $this->normalizeString($controller);
75
76 11
        $templateName = sprintf('%s-%s', $module, $controller);
77 11
        if (!empty($action)) {
78 9
            $templateName = sprintf(
79 9
                '%s-%s',
80
                $templateName,
81 9
                $this->normalizeString($action)
82
            );
83
        }
84
85 11
        return sprintf('modules/%s/templates/%s.html.twig', $module, $templateName);
86
    }
87
88
    /**
89
     * @param string $value
90
     * @return string
91
     */
92 11
    private function normalizeString($value)
93
    {
94 11
        return str_replace('\\', '-', mb_strtolower($value));
95
    }
96
}
97