1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
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 Sylius\Bundle\ThemeBundle\Loader; |
13
|
|
|
|
14
|
|
|
use Sylius\Bundle\ThemeBundle\Factory\ThemeFactoryInterface; |
15
|
|
|
use Sylius\Bundle\ThemeBundle\Filesystem\Filesystem; |
16
|
|
|
use Sylius\Bundle\ThemeBundle\Filesystem\FilesystemInterface; |
17
|
|
|
use Symfony\Component\Config\Loader\Loader; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Abstract loader for themes based on files. |
21
|
|
|
* |
22
|
|
|
* @author Kamil Kokot <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
abstract class ThemeLoader extends Loader |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var FilesystemInterface |
28
|
|
|
*/ |
29
|
|
|
private $filesystem; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ThemeFactoryInterface |
33
|
|
|
*/ |
34
|
|
|
private $themeFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param FilesystemInterface $filesystem |
38
|
|
|
* @param ThemeFactoryInterface $themeFactory |
39
|
|
|
*/ |
40
|
|
|
public function __construct(FilesystemInterface $filesystem, ThemeFactoryInterface $themeFactory) |
41
|
|
|
{ |
42
|
|
|
$this->filesystem = $filesystem; |
43
|
|
|
$this->themeFactory = $themeFactory; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function load($resource, $type = null) |
50
|
|
|
{ |
51
|
|
|
if (!$this->filesystem->exists($resource)) { |
52
|
|
|
throw new \InvalidArgumentException(sprintf('Given theme metadata file "%s" does not exists!', $resource)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$themeData = $this->transformResourceContentsToArray($this->filesystem->getFileContents($resource)); |
56
|
|
|
|
57
|
|
|
$theme = $this->themeFactory->createFromArray($themeData); |
58
|
|
|
$theme->setPath(substr($resource, 0, strrpos($resource, '/'))); |
59
|
|
|
|
60
|
|
|
return $theme; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns theme data array from resource contents. |
65
|
|
|
* |
66
|
|
|
* @param string $contents |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
abstract protected function transformResourceContentsToArray($contents); |
71
|
|
|
} |
72
|
|
|
|