PluginProbe
Code Snippets / 3.10.0
Code Snippets v3.10.0
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / vendor / typisttech / imposter / src / Imposter.php

Imposter.php in Code Snippets 3.10.0, at vendor/typisttech/imposter/src/Imposter.php

136 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace TypistTech\Imposter;
6
7 class Imposter implements ImposterInterface
8 {
9 /**
10 * @var string[]
11 */
12 private $autoloads;
13
14 /**
15 * @var string[]
16 */
17 private $invalidAutoloads;
18
19 /**
20 * @var ConfigCollectionInterface
21 */
22 private $configCollection;
23
24 /**
25 * @var TransformerInterface
26 */
27 private $transformer;
28
29 /**
30 * @var FilesystemInterface
31 */
32 private $filesystem;
33
34 /**
35 * Imposter constructor.
36 *
37 * @param ConfigCollectionInterface $configCollection
38 * @param TransformerInterface $transformer
39 * @param FilesystemInterface $filesystem
40 */
41 public function __construct(
42 ConfigCollectionInterface $configCollection,
43 TransformerInterface $transformer,
44 FilesystemInterface $filesystem
45 ) {
46 $this->configCollection = $configCollection;
47 $this->transformer = $transformer;
48 $this->filesystem = $filesystem;
49 }
50
51 /**
52 * @return ConfigCollectionInterface
53 */
54 public function getConfigCollection(): ConfigCollectionInterface
55 {
56 return $this->configCollection;
57 }
58
59 /**
60 * @return TransformerInterface
61 */
62 public function getTransformer(): TransformerInterface
63 {
64 return $this->transformer;
65 }
66
67 /**
68 * Transform all autoload files.
69 *
70 * @return void
71 */
72 public function run()
73 {
74 $autoloads = $this->getAutoloads();
75 array_walk($autoloads, [$this, 'transform']);
76 }
77
78 /**
79 * Get all valid (exist) autoload paths.
80 *
81 * @return string[]
82 */
83 public function getAutoloads(): array
84 {
85 if ($this->autoloads === null) {
86 $this->setAutoloads();
87 }
88
89 return $this->autoloads;
90 }
91
92 /**
93 * Get all autoload paths which defined in composer.json but not exist.
94 *
95 * @return string[]
96 */
97 public function getInvalidAutoloads(): array
98 {
99 if ($this->invalidAutoloads === null) {
100 $this->setAutoloads();
101 }
102
103 return $this->invalidAutoloads;
104 }
105
106 protected function setAutoloads(): void
107 {
108 $this->autoloads = [];
109 $this->invalidAutoloads = [];
110
111 $autoloads = $this->configCollection->getAutoloads();
112
113 foreach ($autoloads as $autoload) {
114 $isValid = $this->filesystem->isFile($autoload) || $this->filesystem->isDir($autoload);
115
116 if ($isValid) {
117 $this->autoloads[] = $autoload;
118 } else {
119 $this->invalidAutoloads[] = $autoload;
120 }
121 }
122 }
123
124 /**
125 * Transform a file or directory recursively.
126 *
127 * @param string $target Path to the target file or directory.
128 *
129 * @return void
130 */
131 public function transform(string $target)
132 {
133 $this->transformer->transform($target);
134 }
135 }
136