|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the puli/repository package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Puli\Repository; |
|
13
|
|
|
|
|
14
|
|
|
use Puli\Repository\Api\Resource\PuliResource; |
|
15
|
|
|
use Puli\Repository\Api\ResourceNotFoundException; |
|
16
|
|
|
use Puli\Repository\Api\ResourceRepository; |
|
17
|
|
|
use Puli\Repository\Api\UnsupportedLanguageException; |
|
18
|
|
|
use Puli\Repository\Resource\LinkResource; |
|
19
|
|
|
use Webmozart\Assert\Assert; |
|
20
|
|
|
use Webmozart\PathUtil\Path; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Abstract base for repositories providing tools to avoid code duplication. |
|
24
|
|
|
* |
|
25
|
|
|
* @since 1.0 |
|
26
|
|
|
* |
|
27
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
28
|
|
|
* @author Titouan Galopin <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract class AbstractRepository implements ResourceRepository |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* Validate a language is usable to search in repositories. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $language |
|
36
|
|
|
*/ |
|
37
|
214 |
|
protected function validateSearchLanguage($language) |
|
38
|
|
|
{ |
|
39
|
214 |
|
if ('glob' !== $language) { |
|
40
|
10 |
|
throw UnsupportedLanguageException::forLanguage($language); |
|
41
|
|
|
} |
|
42
|
204 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Sanitize a given path and check its validity. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $path |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
537 |
|
protected function sanitizePath($path) |
|
52
|
|
|
{ |
|
53
|
537 |
|
Assert::stringNotEmpty($path, 'The path must be a non-empty string. Got: %s'); |
|
54
|
517 |
|
Assert::startsWith($path, '/', 'The path %s is not absolute.'); |
|
55
|
|
|
|
|
56
|
507 |
|
return Path::canonicalize($path); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $path |
|
61
|
|
|
* |
|
62
|
|
|
* @return Resource |
|
|
|
|
|
|
63
|
|
|
*/ |
|
64
|
10 |
|
protected function followLinksOrFail($path) |
|
65
|
|
|
{ |
|
66
|
10 |
|
$resolvedWithLinks = $this->followLinks($path); |
|
67
|
|
|
|
|
68
|
10 |
|
if ($resolvedWithLinks) { |
|
69
|
3 |
|
return $resolvedWithLinks; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
7 |
|
throw ResourceNotFoundException::forPath($path); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Resolve links in the given path to find the resource. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $path |
|
79
|
|
|
* |
|
80
|
|
|
* @return Resource|null Return the found resource or null if nothing was found. |
|
81
|
|
|
*/ |
|
82
|
|
|
abstract protected function followLinks($path); |
|
83
|
|
|
} |
|
84
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.