1 | <?php |
||
2 | |||
3 | namespace Rawilk\LaravelModules\Publishing; |
||
4 | |||
5 | use Illuminate\Console\Command; |
||
6 | use Illuminate\Filesystem\Filesystem; |
||
7 | use Rawilk\LaravelModules\Contracts\Publisher as PublisherContract; |
||
8 | use Rawilk\LaravelModules\Contracts\Repository; |
||
9 | use Rawilk\LaravelModules\Module; |
||
10 | use RuntimeException; |
||
11 | |||
12 | abstract class Publisher implements PublisherContract |
||
13 | { |
||
14 | /** @var \Illuminate\Console\Command */ |
||
15 | protected $console; |
||
16 | |||
17 | /** @var string */ |
||
18 | protected $error = ''; |
||
19 | |||
20 | /** @var string */ |
||
21 | protected $module; |
||
22 | |||
23 | /** @var \Rawilk\LaravelModules\Contracts\Repository */ |
||
24 | protected $repository; |
||
25 | |||
26 | /** @var bool */ |
||
27 | protected $showMessage = true; |
||
28 | |||
29 | /** @var string */ |
||
30 | protected $success; |
||
31 | |||
32 | /** |
||
33 | * @param \Rawilk\LaravelModules\Module $module |
||
34 | */ |
||
35 | public function __construct(Module $module) |
||
36 | { |
||
37 | $this->module = $module; |
||
38 | } |
||
39 | |||
40 | abstract public function getDestinationPath(): string; |
||
41 | |||
42 | abstract public function getSourcePath(): string; |
||
43 | |||
44 | public function getConsole(): Command |
||
45 | { |
||
46 | return $this->console; |
||
47 | } |
||
48 | |||
49 | public function getFilesystem(): Filesystem |
||
50 | { |
||
51 | return $this->repository->getFiles(); |
||
52 | } |
||
53 | |||
54 | public function getModule(): Module |
||
55 | { |
||
56 | return $this->module; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
57 | } |
||
58 | |||
59 | public function getRepository(): Repository |
||
60 | { |
||
61 | return $this->repository; |
||
62 | } |
||
63 | |||
64 | public function hideMessage(): self |
||
65 | { |
||
66 | $this->showMessage = false; |
||
67 | |||
68 | return $this; |
||
69 | } |
||
70 | |||
71 | public function publish(): void |
||
72 | { |
||
73 | if (! $this->console instanceof Command) { |
||
0 ignored issues
–
show
|
|||
74 | $message = "The 'console' property must be an instance of " . Command::class . '.'; |
||
75 | |||
76 | throw new RuntimeException($message); |
||
77 | } |
||
78 | |||
79 | if (! $this->getFilesystem()->isDirectory($sourcePath = $this->getSourcePath())) { |
||
80 | return; |
||
81 | } |
||
82 | |||
83 | if (! $this->getFilesystem()->isDirectory($destinationPath = $this->getDestinationPath())) { |
||
84 | $this->getFilesystem()->makeDirectory($destinationPath, 0775, true); |
||
85 | } |
||
86 | |||
87 | if ($this->getFilesystem()->copyDirectory($sourcePath, $destinationPath)) { |
||
88 | if ($this->showMessage) { |
||
89 | $this->console->line("<info>Published</info>: {$this->module->getStudlyName()}"); |
||
90 | } |
||
91 | } else { |
||
92 | $this->console->error($this->error); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | public function setConsole(Command $console): self |
||
97 | { |
||
98 | $this->console = $console; |
||
99 | |||
100 | return $this; |
||
101 | } |
||
102 | |||
103 | public function setRepository(Repository $repository): self |
||
104 | { |
||
105 | $this->repository = $repository; |
||
106 | |||
107 | return $this; |
||
108 | } |
||
109 | |||
110 | public function showMessage(): self |
||
111 | { |
||
112 | $this->showMessage = true; |
||
113 | |||
114 | return $this; |
||
115 | } |
||
116 | } |
||
117 |