1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace TYPO3\PharStreamWrapper; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the TYPO3 project. |
7
|
|
|
* |
8
|
|
|
* It is free software; you can redistribute it and/or modify it under the terms |
9
|
|
|
* of the MIT License (MIT). For the full copyright and license information, |
10
|
|
|
* please read the LICENSE file that was distributed with this source code. |
11
|
|
|
* |
12
|
|
|
* The TYPO3 project - inspiring people to share! |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
class Behavior implements Assertable |
16
|
|
|
{ |
17
|
|
|
const COMMAND_DIR_OPENDIR = 'dir_opendir'; |
18
|
|
|
const COMMAND_MKDIR = 'mkdir'; |
19
|
|
|
const COMMAND_RENAME = 'rename'; |
20
|
|
|
const COMMAND_RMDIR = 'rmdir'; |
21
|
|
|
const COMMAND_STEAM_METADATA = 'stream_metadata'; |
22
|
|
|
const COMMAND_STREAM_OPEN = 'stream_open'; |
23
|
|
|
const COMMAND_UNLINK = 'unlink'; |
24
|
|
|
const COMMAND_URL_STAT = 'url_stat'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string[] |
28
|
|
|
*/ |
29
|
|
|
private $availableCommands = [ |
30
|
|
|
self::COMMAND_DIR_OPENDIR, |
31
|
|
|
self::COMMAND_MKDIR, |
32
|
|
|
self::COMMAND_RENAME, |
33
|
|
|
self::COMMAND_RMDIR, |
34
|
|
|
self::COMMAND_STEAM_METADATA, |
35
|
|
|
self::COMMAND_STREAM_OPEN, |
36
|
|
|
self::COMMAND_UNLINK, |
37
|
|
|
self::COMMAND_URL_STAT, |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Assertable[] |
42
|
|
|
*/ |
43
|
|
|
private $assertions; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Assertable $assertable |
47
|
|
|
* @param string ...$commands |
48
|
|
|
* @return static |
49
|
|
|
*/ |
50
|
|
|
public function withAssertion(Assertable $assertable, string ...$commands): self |
51
|
|
|
{ |
52
|
|
|
$this->assertCommands($commands); |
53
|
|
|
$commands = $commands ?: $this->availableCommands; |
54
|
|
|
|
55
|
|
|
$target = clone $this; |
56
|
|
|
foreach ($commands as $command) { |
57
|
|
|
$target->assertions[$command] = $assertable; |
58
|
|
|
} |
59
|
|
|
return $target; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $path |
64
|
|
|
* @param string $command |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function assert(string $path, string $command): bool |
68
|
|
|
{ |
69
|
|
|
$this->assertCommand($command); |
70
|
|
|
$this->assertAssertionCompleteness(); |
71
|
|
|
|
72
|
|
|
return $this->assertions[$command]->assert($path, $command); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $commands |
77
|
|
|
*/ |
78
|
|
|
private function assertCommands(array $commands) |
79
|
|
|
{ |
80
|
|
|
$unknownCommands = array_diff($commands, $this->availableCommands); |
81
|
|
|
if (empty($unknownCommands)) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
throw new \LogicException( |
85
|
|
|
sprintf( |
86
|
|
|
'Unknown commands: %s', |
87
|
|
|
implode(', ', $unknownCommands) |
88
|
|
|
), |
89
|
|
|
1535189881 |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function assertCommand(string $command) |
94
|
|
|
{ |
95
|
|
|
if (in_array($command, $this->availableCommands, true)) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
throw new \LogicException( |
99
|
|
|
sprintf( |
100
|
|
|
'Unknown command "%s"', |
101
|
|
|
$command |
102
|
|
|
), |
103
|
|
|
1535189882 |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function assertAssertionCompleteness() |
108
|
|
|
{ |
109
|
|
|
$undefinedAssertions = array_diff( |
110
|
|
|
$this->availableCommands, |
111
|
|
|
array_keys($this->assertions) |
112
|
|
|
); |
113
|
|
|
if (empty($undefinedAssertions)) { |
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
throw new \LogicException( |
117
|
|
|
sprintf( |
118
|
|
|
'Missing assertions for commands: %s', |
119
|
|
|
implode(', ', $undefinedAssertions) |
120
|
|
|
), |
121
|
|
|
1535189883 |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|