root / drupal7 / misc / typo3 / phar-stream-wrapper / src / Resolver / PharInvocationCollection.php @ fbb66ca6
1 | fbb66ca6 | Assos Assos | <?php
|
---|---|---|---|
2 | namespace TYPO3\PharStreamWrapper\Resolver; |
||
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 | use TYPO3\PharStreamWrapper\Collectable; |
||
15 | |||
16 | class PharInvocationCollection implements Collectable |
||
17 | { |
||
18 | const UNIQUE_INVOCATION = 1; |
||
19 | const UNIQUE_BASE_NAME = 2; |
||
20 | const DUPLICATE_ALIAS_WARNING = 32; |
||
21 | |||
22 | /**
|
||
23 | * @var PharInvocation[]
|
||
24 | */
|
||
25 | private $invocations = array(); |
||
26 | |||
27 | /**
|
||
28 | * @param PharInvocation $invocation
|
||
29 | * @return bool
|
||
30 | */
|
||
31 | public function has(PharInvocation $invocation) |
||
32 | { |
||
33 | return in_array($invocation, $this->invocations, true); |
||
34 | } |
||
35 | |||
36 | /**
|
||
37 | * @param PharInvocation $invocation
|
||
38 | * @param null|int $flags
|
||
39 | * @return bool
|
||
40 | */
|
||
41 | public function collect(PharInvocation $invocation, $flags = null) |
||
42 | { |
||
43 | if ($flags === null) { |
||
44 | $flags = static::UNIQUE_INVOCATION | static::DUPLICATE_ALIAS_WARNING; |
||
45 | } |
||
46 | if ($invocation->getBaseName() === '' |
||
47 | || $invocation->getAlias() === '' |
||
48 | || !$this->assertUniqueBaseName($invocation, $flags) |
||
49 | || !$this->assertUniqueInvocation($invocation, $flags) |
||
50 | ) { |
||
51 | return false; |
||
52 | } |
||
53 | if ($flags & static::DUPLICATE_ALIAS_WARNING) { |
||
54 | $this->triggerDuplicateAliasWarning($invocation); |
||
55 | } |
||
56 | |||
57 | $this->invocations[] = $invocation; |
||
58 | return true; |
||
59 | } |
||
60 | |||
61 | /**
|
||
62 | * @param callable $callback
|
||
63 | * @param bool $reverse
|
||
64 | * @return null|PharInvocation
|
||
65 | */
|
||
66 | public function findByCallback($callback, $reverse = false) |
||
67 | { |
||
68 | foreach ($this->getInvocations($reverse) as $invocation) { |
||
69 | if (call_user_func($callback, $invocation) === true) { |
||
70 | return $invocation; |
||
71 | } |
||
72 | } |
||
73 | return null; |
||
74 | } |
||
75 | |||
76 | /**
|
||
77 | * Asserts that base-name is unique. This disallows having multiple invocations for
|
||
78 | * same base-name but having different alias names.
|
||
79 | *
|
||
80 | * @param PharInvocation $invocation
|
||
81 | * @param int $flags
|
||
82 | * @return bool
|
||
83 | */
|
||
84 | private function assertUniqueBaseName(PharInvocation $invocation, $flags) |
||
85 | { |
||
86 | if (!($flags & static::UNIQUE_BASE_NAME)) { |
||
87 | return true; |
||
88 | } |
||
89 | return $this->findByCallback( |
||
90 | function (PharInvocation $candidate) use ($invocation) { |
||
91 | return $candidate->getBaseName() === $invocation->getBaseName(); |
||
92 | } |
||
93 | ) === null;
|
||
94 | } |
||
95 | |||
96 | /**
|
||
97 | * Asserts that combination of base-name and alias is unique. This allows having multiple
|
||
98 | * invocations for same base-name but having different alias names (for whatever reason).
|
||
99 | *
|
||
100 | * @param PharInvocation $invocation
|
||
101 | * @param int $flags
|
||
102 | * @return bool
|
||
103 | */
|
||
104 | private function assertUniqueInvocation(PharInvocation $invocation, $flags) |
||
105 | { |
||
106 | if (!($flags & static::UNIQUE_INVOCATION)) { |
||
107 | return true; |
||
108 | } |
||
109 | return $this->findByCallback( |
||
110 | function (PharInvocation $candidate) use ($invocation) { |
||
111 | return $candidate->equals($invocation); |
||
112 | } |
||
113 | ) === null;
|
||
114 | } |
||
115 | |||
116 | /**
|
||
117 | * Triggers warning for invocations with same alias and same confirmation state.
|
||
118 | *
|
||
119 | * @param PharInvocation $invocation
|
||
120 | * @see \TYPO3\PharStreamWrapper\PharStreamWrapper::collectInvocation()
|
||
121 | */
|
||
122 | private function triggerDuplicateAliasWarning(PharInvocation $invocation) |
||
123 | { |
||
124 | $sameAliasInvocation = $this->findByCallback( |
||
125 | function (PharInvocation $candidate) use ($invocation) { |
||
126 | return $candidate->isConfirmed() === $invocation->isConfirmed() |
||
127 | && $candidate->getAlias() === $invocation->getAlias(); |
||
128 | }, |
||
129 | true
|
||
130 | ); |
||
131 | if ($sameAliasInvocation === null) { |
||
132 | return;
|
||
133 | } |
||
134 | trigger_error(
|
||
135 | sprintf(
|
||
136 | 'Alias %s cannot be used by %s, already used by %s',
|
||
137 | $invocation->getAlias(),
|
||
138 | $invocation->getBaseName(),
|
||
139 | $sameAliasInvocation->getBaseName()
|
||
140 | ), |
||
141 | E_USER_WARNING
|
||
142 | ); |
||
143 | } |
||
144 | |||
145 | /**
|
||
146 | * @param bool $reverse
|
||
147 | * @return PharInvocation[]
|
||
148 | */
|
||
149 | private function getInvocations($reverse = false) |
||
150 | { |
||
151 | if ($reverse) { |
||
152 | return array_reverse($this->invocations); |
||
153 | } |
||
154 | return $this->invocations; |
||
155 | } |
||
156 | } |