ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilSkinFactory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 
31 {
34  protected ilLanguage $lng;
35 
36  public function __construct(ilLanguage $lng, ?ilSystemStyleConfig $config = null)
37  {
38  global $DIC;
39  $this->lng = $lng;
40  $this->archives = new Archives();
41 
42  if ($config) {
43  $this->config = $config;
44  } else {
45  $this->config = new ilSystemStyleConfig();
46  }
47  }
48 
53  public function skinFromXML(string $path = ''): ilSkin
54  {
55  try {
56  $xml = new SimpleXMLElement(file_get_contents($path));
57  } catch (Exception $e) {
59  }
60 
61  $id = basename(dirname($path));
62  $skin = new ilSkin($id, (string) $xml->attributes()['name']);
63  $skin->setVersion((string) $xml->attributes()['version']);
64 
68  $last_style = null;
69 
70  foreach ($xml->children() as $style_xml) {
71  $style = ilSkinStyle::parseFromXMLElement($style_xml);
72 
76  if ($style_xml->getName() == 'substyle') {
77  if (!$last_style) {
79  }
80  $style->setSubstyleOf($last_style->getId());
81  } else {
82  $last_style = $style;
83  }
84  $skin->addStyle($style);
85  }
86  return $skin;
87  }
88 
94  public function skinStyleContainerFromId(
95  string $skin_id,
96  ilSystemStyleMessageStack $message_stack
98  if (!$skin_id) {
100  }
101 
102  if ($skin_id != 'default') {
103  return new ilSkinStyleContainer(
104  $this->lng,
105  $this->skinFromXML($this->config->getCustomizingSkinPath() . $skin_id . '/template.xml'),
106  $message_stack,
107  $this->config
108  );
109  } else {
110  return new ilSkinStyleContainer(
111  $this->lng,
112  $this->skinFromXML($this->config->getDefaultTemplatePath()),
113  $message_stack,
114  $this->config,
115  );
116  }
117  }
118 
123  public function skinStyleContainerFromZip(
124  string $import_zip_path,
125  string $name,
126  ilSystemStyleMessageStack $message_stack
128  $original_skin_id = preg_replace('/[^A-Za-z0-9\-_]/', '', rtrim($name, '.zip'));
129  $skin_id = $original_skin_id;
130 
131  while (ilStyleDefinition::skinExists($skin_id, $this->config)) {
132  $skin_id .= 'Copy';
133  }
134 
135  $skin_path = $this->config->getCustomizingSkinPath() . $skin_id;
136  $temp_skin_path = $skin_path."temp";
137  $zip_path = $temp_skin_path . ".zip";
138  rename($import_zip_path, $zip_path);
139 
140  $this->archives->unzip(
141  Streams::ofResource(fopen($zip_path, 'rb')),
142  $this->archives->unzipOptions()
143  ->withZipOutputPath($temp_skin_path)
144  ->withOverwrite(false)
145  ->withDirectoryHandling(ZipDirectoryHandling::KEEP_STRUCTURE)
146  )->extract();
147  unlink($zip_path);
148  rename($temp_skin_path."/".$original_skin_id, $skin_path);
149  rmdir($temp_skin_path);
150  $container = $this->skinStyleContainerFromId($skin_id, $message_stack);
151  $container->getSkin()->setId($skin_id);
152  $container->setSkin($container->getSkin());
153  $container->updateSkin();
154  return $container;
155  }
156 
161  public function copyFromSkinStyleContainer(
163  ilFileSystemHelper $file_system,
164  ilSystemStyleMessageStack $message_stack,
165  string $new_skin_txt_addon = 'Copy'
167  $new_skin_id_addon = '';
168  $new_skin_name_addon = '';
169 
171  $container->getSkin()->getId() . $new_skin_id_addon,
172  $container->getSystemStylesConf()
173  )) {
174  $new_skin_id_addon .= $new_skin_txt_addon;
175  $new_skin_name_addon .= ' ' . $new_skin_txt_addon;
176  }
177 
178  $new_skin_path = rtrim($container->getSkinDirectory(), '/') . $new_skin_id_addon;
179 
180  mkdir($new_skin_path, 0775, true);
181  $file_system->recursiveCopy($container->getSkinDirectory(), $new_skin_path);
182  $skin_container = $this->skinStyleContainerFromId($container->getSkin()->getId() . $new_skin_id_addon, $message_stack);
183  $skin_container->getSkin()->setName($skin_container->getSkin()->getName() . $new_skin_name_addon);
184  $skin_container->getSkin()->setVersion('0.1');
185  $skin_container->updateSkin($skin_container->getSkin());
186  return $skin_container;
187  }
188 }
static parseFromXMLElement(SimpleXMLElement $xml_element)
Factory to create Skin classes holds an manages the basic data of a skin as provide by the template o...
__construct(ilLanguage $lng, ?ilSystemStyleConfig $config=null)
File System Helper, to reduce deps.
ilSystemStyleConfig $config
$path
Definition: ltiservices.php:32
$container
Definition: wac.php:14
setVersion(string $version)
This class is responsible for all file system related actions related actions of a skin such as copyi...
global $DIC
Definition: feed.php:28
skinStyleContainerFromId(string $skin_id, ilSystemStyleMessageStack $message_stack)
Get container class is responsible for all file system related actions related actions of a skin such...
copyFromSkinStyleContainer(ilSkinStyleContainer $container, ilFileSystemHelper $file_system, ilSystemStyleMessageStack $message_stack, string $new_skin_txt_addon='Copy')
Copies a complete Skin.
ilSystemStyleConfig wraps all &#39;constants&#39; to ensure the testability of all classes using those &#39;const...
static skinExists(string $skin_id, ?ilSystemStyleConfig $system_style_config=null)
Check whether a skin exists.
Used to stack messages to be shown to the user.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
skinStyleContainerFromZip(string $import_zip_path, string $name, ilSystemStyleMessageStack $message_stack)
Imports a skin from zip.
ilSkin holds an manages the basic data of a skin as provide by the template of the skin...
recursiveCopy(string $src, string $dest)
Recursive copy of a folder.