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