1 | <?php |
||
15 | class AssetManager |
||
16 | { |
||
17 | /** |
||
18 | * @var null[] map where asset package class-name => NULL |
||
19 | */ |
||
20 | protected $class_names = []; |
||
21 | |||
22 | /** |
||
23 | * @var AssetInjection[] list of injected, anonymous asset packages |
||
24 | */ |
||
25 | protected $injections = []; |
||
26 | |||
27 | /** |
||
28 | * @var Closure[] list of callbacks for peppering packages |
||
29 | */ |
||
30 | protected $peppering = []; |
||
31 | |||
32 | /** |
||
33 | * Add a given package, including (recursively) the dependencies of that package. |
||
34 | * |
||
35 | * Adding the same package more than once has no effect. |
||
36 | * |
||
37 | * The order in which packages are added also has no effect. |
||
38 | * |
||
39 | * @param string $class_name fully-qualified class-name of asset package class |
||
40 | * |
||
41 | * @return void |
||
42 | */ |
||
43 | 1 | public function add($class_name) |
|
47 | |||
48 | /** |
||
49 | * Directly inject an anonymous asset package. |
||
50 | * |
||
51 | * This lends itself well to things like initialization scripts in controllers or views. |
||
52 | * |
||
53 | * Internally, this is treated just a class-based package, in terms of sorting the injected |
||
54 | * package according to it's dependencies; the only difference is, you can't refer to an |
||
55 | * injected package as a dependency anywhere else. |
||
56 | * |
||
57 | * The `$callback` is similar to the {@see AssetPackage::defineAssets()} method, and |
||
58 | * `$dependencies` is similar to {@see AssetPackage::getDependencies()}. |
||
59 | * |
||
60 | * @param callable $callback asset definition callback - function ($model) : void |
||
61 | * @param string[] $dependencies list of fully-qualified class-names of package dependencies |
||
62 | * |
||
63 | * @return void |
||
64 | */ |
||
65 | 1 | public function inject(callable $callback, array $dependencies = []) |
|
69 | |||
70 | /** |
||
71 | * Populate the given asset model by creating and sorting packages, and then |
||
72 | * applying {@see AssetPackage::defineAssets()} of every package to the given model. |
||
73 | * |
||
74 | * @param object $model asset model |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | 1 | public function populate($model) |
|
94 | |||
95 | /** |
||
96 | * Pepper a created AssetPackage using a callback function - this will be called |
||
97 | * when you {@see populate()} your asset model, before calling the |
||
98 | * {@see AssetPackage::defineAssets()} functions of every added package. |
||
99 | * |
||
100 | * The given function must accept precisely one argument (and should return nothing) |
||
101 | * and must be type-hinted to specify which package you wish to pepper. |
||
102 | * |
||
103 | * @param Closure $callback function (PackageType $package) : void |
||
104 | * |
||
105 | * @return void |
||
106 | * |
||
107 | * @throws UnexpectedValueException if the given function does not accept precisely one argument |
||
108 | */ |
||
109 | 1 | public function pepper($callback) |
|
127 | |||
128 | /** |
||
129 | * Create all packages |
||
130 | * |
||
131 | * @return AssetPackage[] map of asset packages |
||
132 | */ |
||
133 | 1 | private function createPackages() |
|
169 | |||
170 | /** |
||
171 | * Create an individual package |
||
172 | * |
||
173 | * @param string $class_name package class-name |
||
174 | * |
||
175 | * @return AssetPackage |
||
176 | * |
||
177 | * @throws UnexpectedValueException |
||
178 | */ |
||
179 | 1 | protected function createPackage($class_name) |
|
193 | |||
194 | /** |
||
195 | * Internally sort packages in dependency ("topological") order. |
||
196 | * |
||
197 | * Packages are initially sorted by name, to guarantee a predictable base order, e.g. |
||
198 | * unaffected by the order in which the packages were added. |
||
199 | * |
||
200 | * @param AssetPackage[] $packages map of packages |
||
201 | * |
||
202 | * @return AssetPackage[] sorted map of packages |
||
203 | */ |
||
204 | 1 | private function sortPackages($packages) |
|
235 | |||
236 | /** |
||
237 | * Expose packages to previously added peppering functions. |
||
238 | * |
||
239 | * @param AssetPackage[] $packages |
||
240 | * |
||
241 | * @return void |
||
242 | * |
||
243 | * @see pepper() |
||
244 | */ |
||
245 | 1 | private function pepperPackages($packages) |
|
259 | } |
||
260 |