1
|
|
|
<?php |
2
|
|
|
namespace NamespaceProtector; |
3
|
|
|
|
4
|
|
|
use NamespaceProtector\Analyser; |
5
|
|
|
use Psr\SimpleCache\CacheInterface; |
6
|
|
|
use NamespaceProtector\Config\Config; |
7
|
|
|
use NamespaceProtector\Cache\NullCache; |
8
|
|
|
use NamespaceProtector\Parser\PhpFileParser; |
9
|
|
|
use NamespaceProtector\Scanner\ComposerJson; |
10
|
|
|
use NamespaceProtector\Cache\SimpleFileCache; |
11
|
|
|
use NamespaceProtector\Common\FileSystemPath; |
12
|
|
|
use NamespaceProtector\EnvironmentDataLoader; |
13
|
|
|
use NamespaceProtector\Scanner\FileSystemScanner; |
14
|
|
|
use NamespaceProtector\NamespaceProtectorProcessor; |
15
|
|
|
|
16
|
|
|
class NamespaceProtectorProcessorFactory |
17
|
|
|
{ |
18
|
|
|
private const NAMESPACE_PROTECTOR_CACHE = 'namespace-protector-cache'; |
19
|
|
|
|
20
|
|
|
public function create(Config $config): NamespaceProtectorProcessor |
21
|
|
|
{ |
22
|
|
|
$composerJson = new ComposerJson($config->getPathComposerJson()); |
23
|
|
|
$fileSystem = new FileSystemScanner([$config->getStartPath()]); |
24
|
|
|
$metaDataLoader = new EnvironmentDataLoader($composerJson); |
25
|
|
|
$cacheClass = $this->createCacheObject($config); |
26
|
|
|
$analyser = new Analyser(new PhpFileParser($config, $metaDataLoader, $cacheClass)); |
27
|
|
|
|
28
|
|
|
$namespaceProtectorProcessor = new NamespaceProtectorProcessor( |
29
|
|
|
$composerJson, |
30
|
|
|
$fileSystem, |
31
|
|
|
$analyser, |
32
|
|
|
$metaDataLoader |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
return $namespaceProtectorProcessor; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function createCacheObject(Config $config): CacheInterface |
39
|
|
|
{ |
40
|
|
|
if ($config->enabledCache()) { |
41
|
|
|
$directory = \sys_get_temp_dir().self::NAMESPACE_PROTECTOR_CACHE; |
42
|
|
|
|
43
|
|
|
return new SimpleFileCache(new FileSystemPath($directory, true)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return new NullCache(); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|