1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Author: panosru |
7
|
|
|
* Date: 23/04/2018 |
8
|
|
|
* Time: 22:11 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Omega\FaultManager\Traits; |
12
|
|
|
|
13
|
|
|
use League\Flysystem\Filesystem; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Trait FaultGenerator |
17
|
|
|
* @package Omega\FaultManager\Traits |
18
|
|
|
*/ |
19
|
|
|
trait FaultGenerator |
20
|
|
|
{ |
21
|
|
|
/** @var string */ |
22
|
|
|
private static $compiledPath; |
23
|
|
|
|
24
|
|
|
/** @var Filesystem */ |
25
|
|
|
private static $filesystem; |
26
|
|
|
|
27
|
|
|
/** @var bool */ |
28
|
|
|
private static $autoload = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public static function setCompilePath(string $path): void |
34
|
|
|
{ |
35
|
|
|
// Add trailing slash in case is not present (the shortcut way) |
36
|
|
|
$path = \rtrim($path, \DIRECTORY_SEPARATOR) . \DIRECTORY_SEPARATOR; |
37
|
|
|
|
38
|
|
|
if (!(\file_exists($path) && \is_dir($path) && \is_writable($path))) { |
39
|
|
|
throw new \Omega\FaultManager\Exceptions\InvalidCompilePathException($path); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
self::$compiledPath = $path; |
43
|
|
|
self::getFileSystem()->getAdapter()->setPathPrefix($path); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public static function getCompilePath(): string |
50
|
|
|
{ |
51
|
|
|
return self::$compiledPath ?? \dirname(__DIR__, 2) . '/_compiled/'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public static function autoloadCompiledExceptions(): void |
58
|
|
|
{ |
59
|
|
|
self::$autoload = true; |
60
|
|
|
|
61
|
|
|
/** @var \Generator $files */ |
62
|
|
|
$files = self::getFileSystem()->getCompiledExceptions(); |
63
|
|
|
while ($file = $files->current()) { |
64
|
|
|
self::loadCustomException($file); |
65
|
|
|
$files->next(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
public static function autoloadEnabled(): bool |
73
|
|
|
{ |
74
|
|
|
return self::$autoload; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return Filesystem |
79
|
|
|
* @throws \Omega\FaultManager\Exceptions\InvalidCompilePathException |
80
|
|
|
*/ |
81
|
|
|
private static function getFileSystem(): Filesystem |
82
|
|
|
{ |
83
|
|
|
if (null === self::$filesystem) { |
84
|
|
|
// That is trigger by autoloader, thus is not available for testing in unit tests |
85
|
|
|
// but that's not big of a deal since if it was faulty then nothing would work :P |
86
|
|
|
// @codeCoverageIgnoreStart |
87
|
|
|
try { |
88
|
|
|
self::$filesystem = new Filesystem(new \League\Flysystem\Adapter\Local(self::getCompilePath())); |
89
|
|
|
self::$filesystem->addPlugin(new \Omega\FaultManager\Plugins\CompiledExceptions()); |
90
|
|
|
} catch (\LogicException $exception) { |
91
|
|
|
throw new \Omega\FaultManager\Exceptions\InvalidCompilePathException(self::getCompilePath()); |
92
|
|
|
} |
93
|
|
|
// @codeCoverageIgnoreEnd |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return self::$filesystem; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $file |
101
|
|
|
* @param string $contents |
102
|
|
|
* @throws \Omega\FaultManager\Exceptions\InvalidCompilePathException |
103
|
|
|
*/ |
104
|
|
|
private static function persistFile(string $file, string $contents): void |
105
|
|
|
{ |
106
|
|
|
self::getFileSystem()->put(self::makeFileName($file), $contents); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $path |
111
|
|
|
*/ |
112
|
|
|
private static function loadCustomException(string $path): void |
113
|
|
|
{ |
114
|
|
|
require_once $path; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $name |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
private static function makeFileName(string $name): string |
122
|
|
|
{ |
123
|
|
|
return $name.'.php'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $name |
128
|
|
|
* @param string $extend |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
private static function generateFileCode(string $name, string $extend): string |
132
|
|
|
{ |
133
|
|
|
return (\Zend\Code\Generator\FileGenerator::fromArray([ |
134
|
|
|
'class' => (new \Zend\Code\Generator\ClassGenerator()) |
135
|
|
|
->setName($name) |
136
|
|
|
->setExtendedClass($extend) |
137
|
|
|
]))->generate(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|