|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class FileCreationTransaction |
|
9
|
|
|
* |
|
10
|
|
|
* This class will handle keeping track of created files and then if we have a fatal error, |
|
11
|
|
|
* it will allow us to more easily remove the created files so we are not left with broken generated code |
|
12
|
|
|
* |
|
13
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator |
|
14
|
|
|
*/ |
|
15
|
|
|
class FileCreationTransaction |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array List of paths that have been created |
|
19
|
|
|
*/ |
|
20
|
|
|
private static $pathsCreated = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var bool Have we registered the shutdown function |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $registered = false; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var float Time the first file was created in float unix time |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $startTime = 0.0; |
|
31
|
|
|
|
|
32
|
|
|
public static function getTransaction(): array |
|
33
|
|
|
{ |
|
34
|
|
|
return self::$pathsCreated; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $path The absolute path to the created file or folder |
|
39
|
|
|
* |
|
40
|
|
|
* @throws DoctrineStaticMetaException if the path does not exist |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function setPathCreated(string $path): void |
|
43
|
|
|
{ |
|
44
|
|
|
if (!self::$registered) { |
|
45
|
|
|
self::$registered = self::registerShutdownFunction(); |
|
46
|
|
|
} |
|
47
|
|
|
$realPath = realpath($path); |
|
48
|
|
|
if (!$realPath) { |
|
49
|
|
|
throw new DoctrineStaticMetaException("path $path does not seem to exist"); |
|
50
|
|
|
} |
|
51
|
|
|
self::$pathsCreated[$realPath] = $realPath; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Registers our shutdown function. Will attempt to echo out the dirty file clean up commands on a fatal error |
|
56
|
|
|
* |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
|
|
private static function registerShutdownFunction(): bool |
|
60
|
|
|
{ |
|
61
|
|
|
self::$startTime = microtime(true); |
|
62
|
|
|
register_shutdown_function( |
|
63
|
|
|
function () { |
|
64
|
|
|
$error = error_get_last(); |
|
65
|
|
|
if ($error === E_ERROR && count(self::$pathsCreated) > 0) { |
|
66
|
|
|
self::echoDirtyTransactionCleanupCommands(); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Echos out bash find commands to find and delete created paths |
|
76
|
|
|
* |
|
77
|
|
|
* @param bool|resource $handle |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function echoDirtyTransactionCleanupCommands($handle = STDERR): void |
|
80
|
|
|
{ |
|
81
|
|
|
if (0 === count(self::$pathsCreated)) { |
|
82
|
|
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
$sinceTimeSeconds = ceil(microtime(true) - self::$startTime); |
|
85
|
|
|
// why, because of xdebug break - you could easily spend over 1 minute stepping through |
|
86
|
|
|
$sinceTimeMinutes = ceil($sinceTimeSeconds / 60); |
|
87
|
|
|
$pathsToSearch = []; |
|
88
|
|
|
foreach (self::$pathsCreated as $path) { |
|
89
|
|
|
$realPath = realpath($path); |
|
90
|
|
|
if ($realPath) { |
|
91
|
|
|
$pathsToSearch[$realPath] = $realPath; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
if (0 === count($pathsToSearch)) { |
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
$findCommand = 'find ' . implode(' ', $pathsToSearch) . " -mmin -$sinceTimeMinutes"; |
|
98
|
|
|
$line = str_repeat('-', 15); |
|
99
|
|
|
$deleteCommand = "$findCommand -exec rm -rf"; |
|
100
|
|
|
fwrite( |
|
101
|
|
|
$handle, |
|
|
|
|
|
|
102
|
|
|
"\n$line\n" |
|
103
|
|
|
. "\n\nUnclean File Creation Transaction:" |
|
104
|
|
|
. "\n\nTo find created files:\n$findCommand" |
|
105
|
|
|
. "\n\nTo remove created files:\n$deleteCommand" |
|
106
|
|
|
. "\n\n$line\n\n" |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* If the transaction is successful, we can clear out our log of created files |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function markTransactionSuccessful(): void |
|
114
|
|
|
{ |
|
115
|
|
|
self::$pathsCreated = []; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|