-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile-operations
executable file
·136 lines (125 loc) · 3.96 KB
/
file-operations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env php
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Johmanx10\Transaction\Formatter\RollbackFormatter;
use Johmanx10\Transaction\OperationInterface;
use Johmanx10\Transaction\Exception\TransactionRolledBackException;
use Johmanx10\Transaction\Transaction;
use Johmanx10\Transaction\Operation;
$appDir = __DIR__ . '/my-app';
$consoleSource = realpath(__DIR__ . '/../vendor/bin/composer');
$consoleDestination = $appDir . '/bin/console';
$transaction = new Transaction(
// Create the app directory.
new Operation(
// Create the new directory.
function () use ($appDir) {
if (!file_exists($appDir) && !@mkdir($appDir)) {
throw new RuntimeException(
sprintf('Could not create directory: "%s"', $appDir)
);
}
},
// Roll back the operation.
function () use ($appDir) {
if (file_exists($appDir) && !@rmdir($appDir)) {
throw new RuntimeException(
sprintf('Could not remove directory: "%s"', $appDir)
);
}
},
sprintf('Create directory: "%s"', $appDir)
),
// Copy the application console.
// This operation should fail, because the destination directory does not
// exist.
new Operation(
// Copy the application console.
function () use ($consoleSource, $consoleDestination) {
if (!file_exists($consoleDestination)
&& !@copy($consoleSource, $consoleDestination)
) {
throw new RuntimeException(
sprintf(
'Could not copy "%s" -> "%s".',
$consoleSource,
$consoleDestination
)
);
}
},
// Remove the application console.
function () use ($consoleDestination) {
if (file_exists($consoleDestination)
&& !@unlink($consoleDestination)
) {
throw new RuntimeException(
sprintf('Could not remove "%s".', $consoleDestination)
);
}
},
sprintf('Copy "%s" -> "%s"', $consoleSource, $consoleDestination)
),
// Apply executable rights.
new class ($consoleDestination) implements OperationInterface
{
/** @var string */
private $file;
/** @var int */
private $mode;
/**
* Constructor.
*
* @param string $file
*/
public function __construct(string $file)
{
$this->file = $file;
}
/**
* Execute the operation.
*
* @return void
*/
public function __invoke(): void
{
$this->mode = fileperms($this->file);
if (!@chmod($this->file, 0755)) {
throw new RuntimeException(
sprintf('Failed making "%s" executable.', $this->file)
);
}
}
/**
* Apply the rollback mechanism.
*
* @return void
*/
public function rollback(): void
{
if (is_int($this->mode)
&& file_exists($this->file)
&& !@chmod($this->file, $this->mode)
) {
throw new RuntimeException(
sprintf(
'Failed restoring rights of "%s" to %o',
$this->file,
$this->mode
)
);
}
}
}
);
try {
$transaction->commit();
} catch (TransactionRolledBackException $rollback) {
$rollbackFormatter = new RollbackFormatter();
echo $rollbackFormatter->format($rollback) . PHP_EOL;
}
echo PHP_EOL . sprintf(
'Does app directory ("%s") exist, currently? %s',
$appDir,
file_exists($appDir) ? 'Yes' : 'No'
) . PHP_EOL;