19declare(strict_types=1);
28 public const REGEXP =
'%^/[A-Za-z0-9_]+(?:[.-][A-Za-z0-9_]+)*(?:/[A-Za-z0-9_]+(?:[.-][A-Za-z0-9_]+)*)*$%';
38 $this->
insertInto($this->assets, explode(
"/", $asset->getTarget()), $asset);
44 $key = array_shift(
$path);
45 $key_exists = array_key_exists($key,
$assets);
46 $target_reached = count(
$path) === 0;
48 if (!$key_exists && $target_reached) {
53 if (!$target_reached && (!$key_exists || is_array(
$assets[$key]))) {
63 $first_asset = array_shift($first_asset);
66 throw new \LogicException(
67 "There are (at least) two assets for the same target '{$asset->getTarget()}': " .
68 "'{$first_asset->getSource()}' and '{$asset->getSource()}'"
80 if (!preg_match(self::REGEXP, $ilias_base)) {
81 throw new \InvalidArgumentException(
82 "'{$ilias_base}' is not a valid path to ILIAS base folder."
85 if (!preg_match(self::REGEXP, $target)) {
86 throw new \InvalidArgumentException(
87 "'{$target}' is not a valid target path for public assets."
91 $this->
purge($target, array_map(fn(
string $v):
string => $target .
"/" . $v, self::DONT_PURGE));
98 if (is_array($asset)) {
102 $targets = explode(
"/", $asset->getTarget());
103 $this->
copy(
"$ilias_base/{$asset->getSource()}",
"$target");
109 foreach (
$assets as $key => $asset) {
114 protected function copy(
string $source,
string $target): void
116 if (is_file($source)) {
117 copy($source, $target);
118 } elseif (is_dir($source)) {
119 $dir = new \RecursiveDirectoryIterator($source, \FilesystemIterator::SKIP_DOTS);
121 foreach ($dir as
$d) {
122 $name =
$d->getBasename();
123 $this->
copy(
"$source/$name",
"$target/$name");
126 throw new \RuntimeException(
127 "Cannot copy $source, not a file or directory."
132 protected function purge(
string $path, array $dont_purge): bool
134 if (in_array(
$path, $dont_purge)) {
138 if (!file_exists(
$path)) {
142 if (is_file(
$path)) {
149 foreach (array_diff(scandir(
$path), [
'.',
'..']) as $item) {
150 $purged = $this->
purge($path .
"/" . $item, $dont_purge) && $purged;
158 throw new \LogicException(
"Don't know how to purge $path");
163 if (!file_exists(
$path)) {
Will take care of the public assets, just like a good manager does.
buildPublicFolder(string $ilias_base, string $target)
addAssets(PublicAsset ... $assets)
insertInto(array &$assets, array $path, PublicAsset $asset)
copy(string $source, string $target)
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.