Test Setup Failed
Push — master ( a3b2d5...39ac68 )
by Php Easy Api
04:18 queued 13s
created

GeneratorAbstract::replacementVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 5
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Resta\Support\Generator;
4
5
use Resta\Support\Utils;
6
use Resta\Support\Filesystem;
7
8
abstract class GeneratorAbstract
9
{
10
    /**
11
     * @var null|array
12
     */
13
    protected $classProperties;
14
15
    /**
16
     * @var null|string
17
     */
18
    protected $data;
19
20
    /**
21
     * @var null|string
22
     */
23
    protected $file;
24
25
    /**
26
     * @var Filesystem
27
     */
28
    protected $fileSystem;
29
30
    /**
31
     * @var null|string
32
     */
33
    protected $path;
34
35
    /**
36
     * @var null|string
37
     */
38
    protected $name;
39
40
    /**
41
     * @var null|string
42
     */
43
    protected $namespace;
44
45
    /**
46
     * @var string
47
     */
48
    protected $type = 'class';
49
50
    /**
51
     * @var null|string
52
     */
53
    protected $stubPath;
54
55
    /**
56
     * @var array
57
     */
58
    protected $accessibleProperties = array();
59
60
    /**
61
     * @var array
62
     */
63
    protected $methodParameters = array();
64
65
    /**
66
     * @var array
67
     */
68
    protected $loaded = [];
69
70
    /**
71
     * @var null|object
72
     */
73
    protected static $instance;
74
75
    /**
76
     * GeneratorAbstract constructor.
77
     * @param $path
78
     * @param $name
79
     * @param Filesystem $fileSystem
80
     */
81
    public function __construct($path,$name,Filesystem $fileSystem=null)
82
    {
83
        $this->path = $path;
84
85
        $this->name = $name;
86
87
        $this->file = $this->path.''.DIRECTORY_SEPARATOR.''.ucfirst($this->name).'.php';
88
89
        $this->fileSystem = (is_null($fileSystem)) ? files() : $fileSystem;
90
91
        $this->namespace = Utils::getNamespace($this->path);
92
93
        $this->setStubPath();
94
95
        $this->createPath();
96
    }
97
98
    /**
99
     * creates directory for generator
100
     *
101
     * @return mixed|void
102
     */
103
    public function createPath()
104
    {
105
        if(!file_exists($this->path)){
106
            if(!$this->fileSystem->makeDirectory($this->path)){
107
                throw new \Error($this->path.' makeDirectory fail');
108
            }
109
        }
110
    }
111
112
    /**
113
     * get class instance for generator
114
     *
115
     * @return mixed|void
116
     */
117
    protected function getClassInstance()
118
    {
119
        if(!isset($this->loaded['createClass'])){
120
121
            if(is_null(self::$instance)){
122
                $class = Utils::getNamespace($this->file);
123
                self::$instance = new $class;
124
            }
125
        }
126
127
        return self::$instance;
128
    }
129
130
    /**
131
     * get accessible method value for generator
132
     *
133
     * @param $method
134
     * @return mixed|string
135
     */
136
    protected function getAccessibleMethodValue($method)
137
    {
138
        return  (isset($this->accessibleProperties[$method])) ?
139
            $this->accessibleProperties[$method]
140
            : 'public';
141
    }
142
143
    /**
144
     * get class string from generator
145
     *
146
     * @return null|string
147
     *
148
     * @throws \Resta\Exception\FileNotFoundException
149
     */
150
    protected function getClassString()
151
    {
152
        if(preg_match('@class\s.*\n{@',$this->fileSystem->get($this->file),$parse)){
153
            return $parse[0];
154
        }
155
156
        return null;
157
    }
158
159
    /**
160
     * get eval value
161
     *
162
     * @param null $eval
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $eval is correct as it would always require null to be passed?
Loading history...
163
     * @return string
164
     */
165
    protected function getEval($eval=null)
166
    {
167
        return __DIR__ . '/stubs/evals/' .$eval.'.stub';
168
    }
169
170
    /**
171
     * get parameters method value for generator
172
     *
173
     * @param $method
174
     * @param bool $regexEscape
175
     * @return mixed|string
176
     */
177
    protected function getMethodParameters($method,$regexEscape=true)
178
    {
179
        return  (isset($this->methodParameters[$method])) ?
180
            ($regexEscape) ? $this->regexEscape($this->methodParameters[$method]) : $this->methodParameters[$method]
181
            : '';
182
    }
183
184
    /**
185
     * get stub file for generator
186
     *
187
     * @return string
188
     */
189
    public function getStubFile()
190
    {
191
        $stubFile = $this->stubPath.''.DIRECTORY_SEPARATOR.''.$this->type.'.stub';
192
193
        if(!file_exists($stubFile)){
194
            throw new \Error($stubFile.' path is not available');
195
        }
196
197
        return $stubFile;
198
    }
199
200
    /**
201
     * escapes to regex data for generator
202
     *
203
     * @param $data
204
     * @return mixed
205
     */
206
    protected function regexEscape($data)
207
    {
208
        $dataEscape = str_replace('\\','\\\\',$data);
209
        $dataEscape = str_replace('$','\$',$dataEscape);
210
        $dataEscape = str_replace('()','\(\)',$dataEscape);
211
        $dataEscape = str_replace('[]','\[\]',$dataEscape);
212
213
214
        return $dataEscape;
215
    }
216
217
    /**
218
     * replace with replacement variables content of the given file
219
     *
220
     * @param $replacement
221
     * @param $file
222
     * @param bool $default
223
     * @return void
224
     *
225
     * @throws \Resta\Exception\FileNotFoundException
226
     */
227
    protected function replaceFileContent($replacement,$file,$default=false)
228
    {
229
        $replacementVariables = ($default) ? $replacement : $this->replacementVariables($replacement);
230
        $content = $this->fileSystem->get($file);
231
232
        foreach ($replacementVariables as $key=>$replacementVariable){
233
234
            if($default){
235
                $search = '/'.$key.'/';
236
            }
237
            else{
238
                $search = '/__'.$key.'__/';
239
            }
240
241
            $replace = $replacementVariable;
242
            $content = preg_replace($search,$replace,$content);
243
        }
244
        $this->fileSystem->replace($file,$content);
245
    }
246
247
    /**
248
     * get replacement variables
249
     *
250
     * @param array $replacement
251
     * @return array
252
     */
253
    protected function replacementVariables($replacement=array())
254
    {
255
        $replacement['namespace'] = $this->namespace;
256
        $replacement['class'] = $this->name;
257
258
        return array_map(function($item){
259
            return ucfirst($item);
260
        },$replacement);
261
    }
262
263
    /**
264
     * set stub path
265
     *
266
     * @param $stubPath
267
     */
268
    public function setStubPath($stubPath=null)
269
    {
270
        if(is_null($stubPath)){
271
            $this->stubPath = __DIR__.'/stubs';
272
        }
273
        else{
274
            $this->stubPath = $stubPath;
275
        }
276
    }
277
}