Passed
Push — master ( 2c1718...50160e )
by Bruno
08:20
created

GeneratorNameTrait::splitClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Modelarium;
4
5
use Doctrine\Inflector\InflectorFactory;
6
use Illuminate\Support\Str;
7
use LightnCandy\LightnCandy;
8
use Modelarium\Exception\Exception;
9
10
trait GeneratorNameTrait
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $name = '';
16
17
    /**
18
     * @var string
19
     */
20
    protected $studlyName = '';
21
22
    /**
23
     * @var string
24
     */
25
    protected $lowerName = '';
26
27
    /**
28
     * @var string
29
     */
30
    protected $lowerNamePlural = '';
31
32
    public function getInflector(): \Doctrine\Inflector\Inflector
33
    {
34
        static $inflector = null;
35
        if (!$inflector) {
36
            $inflector = InflectorFactory::create()->build();
37
        }
38
        return $inflector;
39
    }
40
41
    protected function setName(string $name): void
42
    {
43
        $this->name = $name;
44
        $this->studlyName = Str::studly($this->name);
45
        $this->lowerName = mb_strtolower($this->name);
46
        $this->lowerNamePlural = $this->getInflector()->pluralize($this->lowerName);
47
    }
48
49
    /**
50
     * Splits a fully qualified class name into its namespace, class name and relative path
51
     *
52
     * @param string $fullclass
53
     * @return array
54
     */
55
    protected static function splitClassName(string $fullclass): array
56
    {
57
        $classTokens = explode('\\', $fullclass);
58
        $className = array_pop($classTokens);
59
        $classNamespace = implode('\\', $classTokens);
60
        $relativePath = implode('/', $classTokens);
61
        return [$classNamespace, $className, $relativePath];
62
    }
63
64
    /**
65
     * Returns the base path (where composer.json is located)
66
     *
67
     * @param string $file The filename
68
     * @return string
69
     */
70
    public static function getBasePath(string $file = null): string
71
    {
72
        $basepath = dirname(\Composer\Factory::getComposerFile());
73
        if ($file) {
74
            $basepath .= '/' . $file;
75
        }
76
        return $basepath;
77
    }
78
79
    /**
80
     * Get the value of name
81
     *
82
     * @return  string
83
     */
84
    public function getName()
85
    {
86
        return $this->name;
87
    }
88
89
    /**
90
     * Get the value of studlyName
91
     *
92
     * @return  string
93
     */
94
    public function getStudlyName()
95
    {
96
        return $this->studlyName;
97
    }
98
99
    /**
100
     *
101
     * @param string $path
102
     * @return callable
103
     */
104
    protected function compileMustacheFromFile(string $path)
105
    {
106
        $template = \Safe\file_get_contents($path);
107
        $phpStr = LightnCandy::compile(
108
            $template,
109
            [
0 ignored issues
show
Bug introduced by
array('flags' => LightnC...' => array('{|', '|}')) of type array<string,array<integer,string>|integer> is incompatible with the type array<string,array|integer|string> expected by parameter $options of LightnCandy\LightnCandy::compile(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
            /** @scrutinizer ignore-type */ [
Loading history...
110
                'flags' => LightnCandy::FLAG_ERROR_EXCEPTION,
111
                'delimiters' => array('{|', '|}')
112
            ]
113
        );
114
        if (!$phpStr) {
115
            throw new Exception('Invalid template');
116
        }
117
        /** @var callable $renderer */
118
        $renderer = LightnCandy::prepare($phpStr);
0 ignored issues
show
Deprecated Code introduced by
The function LightnCandy\LightnCandy::prepare() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

118
        $renderer = /** @scrutinizer ignore-deprecated */ LightnCandy::prepare($phpStr);
Loading history...
119
        return $renderer;
120
    }
121
122
    /**
123
     * Replaces common strings from the stubs
124
     *
125
     * @param string $path The string data to apply replaces
126
     * @param array $context
127
     * @return string
128
     */
129
    public function templateFile(string $path, array $context = [])
130
    {
131
        $renderer = $this->compileMustacheFromFile($path);
132
        $context['StudlyName'] = $context['studlyName'] = $this->studlyName;
133
        $context['lowerName'] = $this->lowerName;
134
        $context['lowerNamePlural'] = $this->lowerNamePlural;
135
        $context['date'] = date("c");
136
137
        return $renderer($context);
138
    }
139
140
    /**
141
     * Stubs from a mustache file. Convenience wrapper for templateFile().
142
     *
143
     * @param string $stubName
144
     * @param array $context
145
     * @return string
146
     */
147
    public function templateStub(string $stubName, array $context = []): string
148
    {
149
        $stub = $this->stubDir . "/$stubName.mustache.php";
150
        return $this->templateFile($stub, $context);
151
    }
152
}
153