ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ImportFileUnzippedFileObjective.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Setup\CLI;
22
23use ZipArchive;
24use ILIAS\Setup;
25use FilesystemIterator;
27use RecursiveIteratorIterator;
29use RecursiveDirectoryIterator;
30
32{
33 protected string $zip_path;
34
35 public function __construct(string $zip_path)
36 {
37 $this->zip_path = $zip_path;
38 }
39
40 public function getHash(): string
41 {
42 return hash("sha256", self::class);
43 }
44
45 public function getLabel(): string
46 {
47 return "Unzip files from $this->zip_path into temporary directory";
48 }
49
50 public function isNotable(): bool
51 {
52 return true;
53 }
54
55 public function getPreconditions(Environment $environment): array
56 {
57 return [
59 ];
60 }
61
62 public function achieve(Environment $environment): Environment
63 {
64 $tmp_dir = $environment->getConfigFor("tmp_dir");
65 $dirs = [
66 $tmp_dir . DIRECTORY_SEPARATOR . "web_data.zip" => $tmp_dir . DIRECTORY_SEPARATOR . "web_data",
67 $tmp_dir . DIRECTORY_SEPARATOR . "Customizing.zip" => $tmp_dir . DIRECTORY_SEPARATOR . "Customizing",
68 $tmp_dir . DIRECTORY_SEPARATOR . "data.zip" => $tmp_dir . DIRECTORY_SEPARATOR . "data",
69 $tmp_dir . DIRECTORY_SEPARATOR . "dump.zip" => $tmp_dir
70 ];
71
72 $this->extractZip($this->zip_path, $tmp_dir);
73
74 foreach ($dirs as $source => $destination) {
75 if (!file_exists($source)) {
76 continue;
77 }
78
79 $this->extractZip($source, $destination);
80
81 $this->deleteRecursive($source);
82 }
83
84 return $environment;
85 }
86
87 public function isApplicable(Environment $environment): bool
88 {
89 return file_exists($this->zip_path);
90 }
91
92 protected function deleteRecursive(string $path, bool $delete_base_dir = false): void
93 {
94 if (is_file($path)) {
95 unlink($path);
96 return;
97 }
98
99 $files = new RecursiveIteratorIterator(
100 new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
101 RecursiveIteratorIterator::CHILD_FIRST
102 );
103
104 foreach ($files as $file_info) {
105 if ($file_info->isDir()) {
106 rmdir($file_info->getRealPath());
107 continue;
108 }
109 unlink($file_info->getRealPath());
110 }
111
112 if ($delete_base_dir) {
113 rmdir($path);
114 }
115 }
116
117 protected function extractZip(string $source, string $destination): void
118 {
119 $zip = new ZipArchive();
120 try {
121 $zip->open($source);
122 $zip->extractTo($destination);
123 } catch (\Exception $e) {
124 throw new Setup\UnachievableException("Could not open zip at $source");
125 } finally {
126 $zip->close();
127 }
128 }
129}
deleteRecursive(string $path, bool $delete_base_dir=false)
achieve(Environment $environment)
Objectives can be achieved.
isNotable()
Get to know if this is an interesting objective for a human.
getPreconditions(Environment $environment)
Objectives might depend on other objectives.
getLabel()
Get a label that describes this objective.
isApplicable(Environment $environment)
Get to know whether the objective is applicable.
Signals that some goal won't be achievable by actions of the system ever.
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
getConfigFor(string $component)
An objective is a desired state of the system that is supposed to be created by the setup.
Definition: Objective.php:31
$path
Definition: ltiservices.php:30
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...