1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of phpDocumentor. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright 2010-2016 Mike van Riel<[email protected]> |
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
10
|
|
|
* @link http://phpdoc.org |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace phpDocumentor\Infrastructure\Renderer; |
14
|
|
|
|
15
|
|
|
use League\Flysystem\File; |
16
|
|
|
use League\Flysystem\Filesystem; |
17
|
|
|
use League\Flysystem\FilesystemInterface; |
18
|
|
|
use League\Flysystem\MountManager; |
19
|
|
|
use phpDocumentor\DomainModel\Path; |
20
|
|
|
use phpDocumentor\DomainModel\Renderer\Asset; |
21
|
|
|
use phpDocumentor\DomainModel\Renderer\AssetNotFoundException; |
22
|
|
|
use phpDocumentor\DomainModel\Renderer\Assets; |
23
|
|
|
use phpDocumentor\DomainModel\Renderer\Template; |
24
|
|
|
|
25
|
|
|
final class FlySystemAssets implements Assets |
26
|
|
|
{ |
27
|
|
|
/** @var FilesystemInterface|MountManager */ |
28
|
|
|
private $filesystem; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param FilesystemInterface|MountManager $filesystem |
32
|
|
|
*/ |
33
|
|
|
public function __construct($filesystem) |
34
|
|
|
{ |
35
|
|
|
$this->filesystem = $filesystem; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritDoc |
40
|
|
|
*/ |
41
|
|
|
public function get(Path $location) |
42
|
|
|
{ |
43
|
|
|
if (! $this->has($location)) { |
44
|
|
|
throw new AssetNotFoundException(sprintf('Asset at "%s" could not found', $location)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** @var File $flyLocation */ |
48
|
|
|
$flyLocation = $this->filesystem->get((string)$location); |
49
|
|
|
if ($flyLocation->isDir()) { |
50
|
|
|
$listing = $this->filesystem->listContents((string)$location, true); |
51
|
|
|
$locations = array_map( |
52
|
|
|
function ($value) { |
53
|
|
|
if ($value['type'] === 'dir') { |
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return new Path($value['path']); |
58
|
|
|
}, |
59
|
|
|
$listing |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
return new Asset\Folder($location, array_values(array_filter($locations))); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return new Asset($flyLocation->read()); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
71
|
|
|
public function has(Path $location) |
72
|
|
|
{ |
73
|
|
|
return $this->filesystem->has((string)$location); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns a new instance of this collection with the assets for the given template included. |
78
|
|
|
* |
79
|
|
|
* @param Template $template |
80
|
|
|
* |
81
|
|
|
* @return Assets |
82
|
|
|
*/ |
83
|
|
|
public function includeTemplateAssets(Template $template) |
84
|
|
|
{ |
85
|
|
|
$assets = clone $this; |
86
|
|
|
if ($assets->filesystem instanceof Filesystem) { |
87
|
|
|
$assets->filesystem = new MountManager([$assets->filesystem]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// TODO: Add template location to the mount manager |
91
|
|
|
// $assets->filesystem->mountFilesystem('', $template->) |
92
|
|
|
|
93
|
|
|
return $assets; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.