Completed
Push — 8.x-1.x ( 05a07c...3dbfad )
by
unknown
39:49
created

TemplateResolver::resolveByControllerAndAction()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
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 22
ccs 12
cts 12
cp 1
rs 8.9197
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 8
    public function resolveByControllerAndAction($controllerClass, $action)
20
    {
21 8
        preg_match('/^Drupal\\\(.*)\\\Controller\\\(.*)/', $controllerClass, $data);
22 8
        if (!empty($data)) {
23 7
            $module = $data[1];
24 7
            $controller = $data[2];
25
        }
26
        else {
27 1
            throw new \InvalidArgumentException(
28 1
              sprintf('Controller class "%s" not supported', $controllerClass)
29
            );
30
        }
31
32 7
        if (preg_match('/^(.+)Controller$/', $controller, $matchController)) {
33 5
            $controller = $matchController[1];
34
        }
35 7
        if (preg_match('/^(.+)Action$/', $action, $matchAction)) {
36 5
            $action = $matchAction[1];
37
        }
38
39 7
        return $this->format($module, $controller, $action);
40
    }
41
42
    /**
43
     * Convert
44
     * "<module>:<controller>"
45
     * and
46
     * "<module>:<controller>:<action>"
47
     * into
48
     * "modules/<module>/templates/<module>-<controller>(-<action>).html.twig"
49
     *
50
     * @param string $template
51
     * @return string
52
     */
53 4
    public function normalize($template)
54
    {
55 4
        if (preg_match('/^(.+):(.+):(.+)$/', $template, $matches)) {
56 1
            return $this->format($matches[1], $matches[2], $matches[3]);
57
        }
58 3
        if (preg_match('/^(.+):(.+)$/', $template, $matches)) {
59 2
            return $this->format($matches[1], $matches[2]);
60
        }
61
62 1
        throw new \InvalidArgumentException(
63 1
          sprintf('Template pattern "%s" not supported', $template)
64
        );
65
    }
66
67
    /**
68
     * @param string $module
69
     * @param string $controller
70
     * @param string $action
71
     * @return string
72
     */
73 10
    private function format($module, $controller, $action = null)
74
    {
75 10
        $controller = $this->normalizeString($controller);
76
77 10
        $templateName = sprintf('%s-%s', $module, $controller);
78 10
        if (!empty($action)) {
79 8
            $templateName = sprintf(
80 8
              '%s-%s',
81 8
              $templateName,
82 8
              $this->normalizeString($action)
83
            );
84
        }
85
86 10
        return sprintf(
87 10
          'modules/%s/templates/%s.html.twig',
88 10
          $module,
89 10
          $templateName
90
        );
91
    }
92
93
    /**
94
     * @param string $value
95
     * @return string
96
     */
97 10
    private function normalizeString($value)
98
    {
99 10
        return str_replace('\\', '-', mb_strtolower($value));
100
    }
101
}
102