ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
PublicAssetManager.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27{
28 public const REGEXP = '%^(/\w+|\w+[\-.]\w+)+$%';
29
30 public const DONT_PURGE = ["data", "Customizing"];
31
32 protected $assets = [];
33
34
35 public function addAssets(PublicAsset ...$assets): void
36 {
37 foreach ($assets as $asset) {
38 $this->insertInto($this->assets, explode("/", $asset->getTarget()), $asset);
39 }
40 }
41
42 protected function insertInto(array &$assets, array $path, PublicAsset $asset): void
43 {
44 $key = array_shift($path);
45 $key_exists = array_key_exists($key, $assets);
46 $target_reached = count($path) === 0;
47
48 if (!$key_exists && $target_reached) {
49 $assets[$key] = $asset;
50 return;
51 }
52
53 if (!$target_reached && (!$key_exists || is_array($assets[$key]))) {
54 if (!$key_exists) {
55 $assets[$key] = [];
56 }
57 $this->insertInto($assets[$key], $path, $asset);
58 return;
59 }
60
61 $first_asset = $assets[$key];
62 while (!$first_asset instanceof PublicAsset) {
63 $first_asset = array_shift($first_asset);
64 }
65
66 throw new \LogicException(
67 "There are (at least) two assets for the same target '{$asset->getTarget()}': " .
68 "'{$first_asset->getSource()}' and '{$asset->getSource()}'"
69 );
70 }
71
76 public function buildPublicFolder(string $ilias_base, string $target): void
77 {
78 if (!preg_match(self::REGEXP, $ilias_base)) {
79 throw new \InvalidArgumentException(
80 "'{$ilias_base}' is not a valid path to ILIAS base folder."
81 );
82 }
83 if (!preg_match(self::REGEXP, $target)) {
84 throw new \InvalidArgumentException(
85 "'{$target}' is not a valid target path for public assets."
86 );
87 }
88
89 $this->purge($target, array_map(fn($v) => $target . "/" . $v, self::DONT_PURGE));
90 $this->makeDir($target);
91 $this->buildPublicFolderRecursivelyArray($ilias_base, $target, $this->assets);
92 }
93
94 protected function buildPublicFolderRecursively(string $ilias_base, string $target, PublicAsset|array $asset): void
95 {
96 if (is_array($asset)) {
97 $this->makeDir("$target");
98 $this->buildPublicFolderRecursivelyArray($ilias_base, $target, $asset);
99 } else {
100 $targets = explode("/", $asset->getTarget());
101 $this->copy("$ilias_base/{$asset->getSource()}", "$target");
102 }
103 }
104
105 protected function buildPublicFolderRecursivelyArray(string $ilias_base, string $target, array $assets): void
106 {
107 foreach ($assets as $key => $asset) {
108 $this->buildPublicFolderRecursively($ilias_base, "$target/$key", $asset);
109 }
110 }
111
112 protected function copy(string $source, string $target): void
113 {
114 if (is_file($source)) {
115 copy($source, $target);
116 } elseif (is_dir($source)) {
117 $dir = new \RecursiveDirectoryIterator($source, \FilesystemIterator::SKIP_DOTS);
118 $this->makeDir($target);
119 foreach ($dir as $d) {
120 $name = $d->getBasename();
121 $this->copy("$source/$name", "$target/$name");
122 }
123 } else {
124 throw new \RuntimeException(
125 "Cannot copy $source, not a file or directory."
126 );
127 }
128 }
129
130 protected function purge(string $path, array $dont_purge): bool
131 {
132 if (in_array($path, $dont_purge)) {
133 return false;
134 }
135
136 if (!file_exists($path)) {
137 return true;
138 }
139
140 if (is_file($path)) {
141 unlink($path);
142 return true;
143 }
144
145 if (is_dir($path)) {
146 $purged = true;
147 foreach (array_diff(scandir($path), ['.', '..']) as $item) {
148 $purged = $this->purge($path . "/" . $item, $dont_purge) && $purged;
149 }
150 if ($purged) {
151 rmdir($path);
152 }
153 return $purged;
154 }
155
156 throw new \LogicException("Don't know how to purge $path");
157 }
158
159 protected function makeDir(string $path): void
160 {
161 if (!file_exists($path)) {
162 mkdir($path, 0755);
163 }
164 }
165}
Will take care of the public assets, just like a good manager does.
buildPublicFolder(string $ilias_base, string $target)
insertInto(array &$assets, array $path, PublicAsset $asset)
buildPublicFolderRecursivelyArray(string $ilias_base, string $target, array $assets)
purge(string $path, array $dont_purge)
buildPublicFolderRecursively(string $ilias_base, string $target, PublicAsset|array $asset)
An public asset is a file or folder that should be served via the web.
Definition: PublicAsset.php:27
$path
Definition: ltiservices.php:30