root / drupal7 / misc / typo3 / phar-stream-wrapper / src / Behavior.php @ cd5c298a
1 |
<?php
|
---|---|
2 |
namespace TYPO3\PharStreamWrapper; |
3 |
|
4 |
/*
|
5 |
* This file is part of the TYPO3 project.
|
6 |
*
|
7 |
* It is free software; you can redistribute it and/or modify it under the terms
|
8 |
* of the MIT License (MIT). For the full copyright and license information,
|
9 |
* please read the LICENSE file that was distributed with this source code.
|
10 |
*
|
11 |
* The TYPO3 project - inspiring people to share!
|
12 |
*/
|
13 |
|
14 |
class Behavior implements Assertable |
15 |
{ |
16 |
const COMMAND_DIR_OPENDIR = 'dir_opendir'; |
17 |
const COMMAND_MKDIR = 'mkdir'; |
18 |
const COMMAND_RENAME = 'rename'; |
19 |
const COMMAND_RMDIR = 'rmdir'; |
20 |
const COMMAND_STEAM_METADATA = 'stream_metadata'; |
21 |
const COMMAND_STREAM_OPEN = 'stream_open'; |
22 |
const COMMAND_UNLINK = 'unlink'; |
23 |
const COMMAND_URL_STAT = 'url_stat'; |
24 |
|
25 |
/**
|
26 |
* @var string[]
|
27 |
*/
|
28 |
private $availableCommands = array( |
29 |
self::COMMAND_DIR_OPENDIR, |
30 |
self::COMMAND_MKDIR, |
31 |
self::COMMAND_RENAME, |
32 |
self::COMMAND_RMDIR, |
33 |
self::COMMAND_STEAM_METADATA, |
34 |
self::COMMAND_STREAM_OPEN, |
35 |
self::COMMAND_UNLINK, |
36 |
self::COMMAND_URL_STAT, |
37 |
); |
38 |
|
39 |
/**
|
40 |
* @var Assertable[]
|
41 |
*/
|
42 |
private $assertions; |
43 |
|
44 |
/**
|
45 |
* @param Assertable $assertable
|
46 |
* @return static
|
47 |
*/
|
48 |
public function withAssertion(Assertable $assertable) |
49 |
{ |
50 |
$commands = func_get_args(); |
51 |
array_shift($commands); |
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($path, $command) |
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($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 |
} |