1
|
|
|
<?php namespace Anomaly\Streams\Platform\Image; |
2
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Config\Repository; |
4
|
|
|
use Illuminate\Contracts\Container\Container; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class ImageMacros |
8
|
|
|
* |
9
|
|
|
* @link http://pyrocms.com/ |
10
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
11
|
|
|
* @author Ryan Thompson <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class ImageMacros |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Registered macros. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $macros; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The service container. |
25
|
|
|
* |
26
|
|
|
* @var Container |
27
|
|
|
*/ |
28
|
|
|
protected $container; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a new ImageMacros instance. |
32
|
|
|
* |
33
|
|
|
* @param Container $container |
34
|
|
|
* @param Repository $config |
35
|
|
|
*/ |
36
|
|
|
public function __construct(Repository $config, Container $container) |
37
|
|
|
{ |
38
|
|
|
$this->macros = $config->get('streams::images.macros', []); |
|
|
|
|
39
|
|
|
$this->container = $container; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Run a macro. |
44
|
|
|
* |
45
|
|
|
* @param $macro |
46
|
|
|
* @param Image $image |
47
|
|
|
* @return Image |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
|
|
public function run($macro, Image $image) |
51
|
|
|
{ |
52
|
|
|
if (!$process = array_get($this->getMacros(), $macro)) { |
53
|
|
|
return $image; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (is_array($process)) { |
57
|
|
|
foreach ($process as $method => $arguments) { |
58
|
|
|
$image->addAlteration($method, $arguments); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($process instanceof \Closure) { |
63
|
|
|
$this->container->call($process, compact('image', 'macro')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $image; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the macros. |
71
|
|
|
* |
72
|
|
|
* @return array|mixed |
73
|
|
|
*/ |
74
|
|
|
public function getMacros() |
75
|
|
|
{ |
76
|
|
|
return $this->macros; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set the macros. |
81
|
|
|
* |
82
|
|
|
* @param array $macros |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function setMacros(array $macros) |
86
|
|
|
{ |
87
|
|
|
$this->macros = $macros; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Add an image macro hint. |
94
|
|
|
* |
95
|
|
|
* @param $namespace |
96
|
|
|
* @param $macro |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function addMacro($macro, $process) |
100
|
|
|
{ |
101
|
|
|
$this->macros[$macro] = $process; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return if a macro exists or not. |
108
|
|
|
* |
109
|
|
|
* @param $macro |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function isMacro($macro) |
113
|
|
|
{ |
114
|
|
|
return isset($this->macros[$macro]); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..