1 | <?php |
||
10 | class Code |
||
11 | { |
||
12 | private $name; |
||
13 | |||
14 | private $className; |
||
15 | |||
16 | private $config; |
||
17 | |||
18 | private $attributes = []; |
||
19 | |||
20 | private $dependencies = []; |
||
21 | |||
22 | private $files; |
||
23 | |||
24 | private $useSortFixer; |
||
25 | |||
26 | 5 | public function __construct($name, $config, $dependencies = [], Filesystem $files = null, UseSortFixer $useSortFixer = null) |
|
27 | { |
||
28 | 5 | $this->files = $files ?: new Filesystem; |
|
29 | 5 | $this->useSortFixer = $useSortFixer ?: new UseSortFixer(); |
|
30 | 5 | $this->useSortFixer->setSortType(UseSortFixer::SORT_TYPE_LENGTH); |
|
31 | 5 | $this->name = $name; |
|
32 | 5 | $this->config = $config; |
|
33 | 5 | $this->dependencies = $dependencies; |
|
34 | |||
35 | 5 | $this->className = $this->name.Arr::get($this->config, 'suffix', ''); |
|
36 | 5 | $this->attributes = $this->mergeAttributes( |
|
37 | 5 | $dependencies, |
|
38 | 5 | Arr::get($this->config, 'plugins', []) |
|
39 | 5 | ); |
|
40 | 5 | } |
|
41 | |||
42 | public function __toString() |
||
46 | |||
47 | 3 | public function getAttributes($prefix = null) |
|
48 | { |
||
49 | 3 | if (empty($prefix) === true) { |
|
50 | return $this->attributes; |
||
51 | } |
||
52 | |||
53 | 3 | $attributes = []; |
|
54 | 3 | foreach ($this->attributes as $key => $value) { |
|
55 | 3 | $attributes[str_replace('-', '_', $prefix.'_'.$key)] = $value; |
|
56 | 3 | } |
|
57 | |||
58 | 3 | return $attributes; |
|
59 | } |
||
60 | |||
61 | 5 | public function render() |
|
65 | |||
66 | 1 | public function store() |
|
67 | { |
||
68 | 1 | foreach ($this->dependencies as $dependency) { |
|
69 | 1 | $dependency->store(); |
|
70 | 1 | } |
|
71 | |||
72 | 1 | $file = Arr::get($this->config, 'path', '').'/'.$this->className.'.'.Arr::get($this->config, 'extension', 'php'); |
|
73 | 1 | $directory = dirname($file); |
|
74 | |||
75 | 1 | if ($this->files->isDirectory($directory) === false) { |
|
76 | 1 | $this->files->makeDirectory($directory, 0755, true); |
|
77 | 1 | } |
|
78 | |||
79 | 1 | return $this->files->exists($file) === false |
|
80 | 1 | ? $this->files->put($file, $this->render()) |
|
81 | 1 | : false; |
|
82 | } |
||
83 | |||
84 | 5 | private function renderStub() |
|
88 | |||
89 | 5 | private function format($content) |
|
95 | |||
96 | 5 | private function mergeAttributes($dependencies, $plugins) |
|
97 | { |
||
98 | 5 | $attributes = array_merge(Arr::get($this->config, 'attributes', []), [ |
|
140 | |||
141 | 5 | private function getDummyAttributes() |
|
155 | } |
||
156 |