|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmaTeam\Image\Projection\Tile; |
|
4
|
|
|
|
|
5
|
|
|
use AmaTeam\Image\Projection\API\Tile\TileInterface; |
|
6
|
|
|
use AmaTeam\Image\Projection\API\Type\MappingInterface; |
|
7
|
|
|
use AmaTeam\Image\Projection\Filesystem\Locator; |
|
8
|
|
|
use AmaTeam\Image\Projection\Filesystem\Pattern; |
|
9
|
|
|
use AmaTeam\Image\Projection\Image\Manager; |
|
10
|
|
|
use League\Flysystem\FilesystemInterface; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
use Psr\Log\NullLogger; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Loads tiles from filesystem by specified pattern. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Loader |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var Manager |
|
21
|
|
|
*/ |
|
22
|
|
|
private $manager; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Locator |
|
26
|
|
|
*/ |
|
27
|
|
|
private $locator; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var LoggerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $logger; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param Manager $manager |
|
35
|
|
|
* @param FilesystemInterface $filesystem |
|
36
|
|
|
* @param LoggerInterface|null $logger |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct( |
|
39
|
|
|
Manager $manager, |
|
40
|
|
|
FilesystemInterface $filesystem, |
|
41
|
|
|
LoggerInterface $logger = null |
|
42
|
|
|
) { |
|
43
|
|
|
$this->manager = $manager; |
|
44
|
|
|
$this->locator = new Locator($filesystem); |
|
45
|
|
|
$this->logger = $logger ?: new NullLogger(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param Pattern $pattern |
|
50
|
|
|
* @return TileInterface[] |
|
51
|
|
|
*/ |
|
52
|
|
|
public function load(Pattern $pattern) |
|
53
|
|
|
{ |
|
54
|
|
|
$context = ['pattern' => $pattern]; |
|
55
|
|
|
$this->logger->debug('Loading tiles by pattern {pattern}', $context); |
|
56
|
|
|
$tiles = array_map(function ($entry) { |
|
57
|
|
|
return $this->createTile($entry['path'], $entry['parameters']); |
|
58
|
|
|
}, $this->locator->locate($pattern)); |
|
59
|
|
|
$context = ['amount' => sizeof($tiles)]; |
|
60
|
|
|
$this->logger->debug('Loaded {amount} tiles', $context); |
|
61
|
|
|
return $tiles; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $path |
|
66
|
|
|
* @param string[] $parameters |
|
67
|
|
|
* @return TileInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
private function createTile($path, array $parameters) |
|
70
|
|
|
{ |
|
71
|
|
|
$x = (int) self::lookup($parameters, ['x', 'h'], 0); |
|
72
|
|
|
$y = (int) self::lookup($parameters, ['y', 'v'], 0); |
|
73
|
|
|
$defaultFace = MappingInterface::DEFAULT_FACE; |
|
74
|
|
|
$face = self::lookup($parameters, ['face', 'f'], $defaultFace); |
|
75
|
|
|
$position = new Position($face, $x, $y); |
|
76
|
|
|
$context = ['position' => $position]; |
|
77
|
|
|
$this->logger->debug('Loading tile {position}', $context); |
|
78
|
|
|
return new Tile($position, $this->manager->read($path)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string[] $source |
|
83
|
|
|
* @param string[] $names |
|
84
|
|
|
* @param mixed $default |
|
85
|
|
|
* @return string|mixed |
|
86
|
|
|
*/ |
|
87
|
|
|
private static function lookup(array $source, array $names, $default = null) |
|
88
|
|
|
{ |
|
89
|
|
|
foreach ($names as $name) { |
|
90
|
|
|
if (isset($source[$name])) { |
|
91
|
|
|
return $source[$name]; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
return $default; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|