Open Data Catalog v2.0.0
Drush.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drush\Commands\DrushCommands;
6 use Masterminds\HTML5;
7 use Symfony\Component\Yaml\Yaml;
8 use Masterminds\HTML5\Parser\FileInputStream;
9 
15 class Drush extends DrushCommands {
16 
17  private $moduleDirectory;
18  private $librariesFilePath;
19  private $reactAppPath;
20  private $reactAppBuildDirectoryPath;
21  private $reactAppBuildStaticJsDirectoryPath;
22  private $reactAppBuildStaticCssDirectoryPath;
23 
27  public function __construct() {
28  $this->moduleDirectory = DRUPAL_ROOT . "/" . drupal_get_path("module", "dkan_metadata_form");
29  $this->librariesFilePath = $this->moduleDirectory . "/dkan_metadata_form.libraries.yml";
30  $this->reactAppPath = $this->moduleDirectory . "/js/app";
31  $this->reactAppBuildDirectoryPath = $this->reactAppPath . "/build";
32  $this->reactAppBuildStaticJsDirectoryPath = $this->reactAppBuildDirectoryPath . "/static/js";
33  $this->reactAppBuildStaticCssDirectoryPath = $this->reactAppBuildDirectoryPath . "/static/css";
34  }
35 
43  public function sync() {
44  $this->createLoadMeJs();
45  $this->createtLibrariesFile();
46  }
47 
51  private function createtLibrariesFile() {
52  $this->removeExistingFile();
53 
54  $libraries = $this->getLibrariesBasicStructure();
55 
56  $paths = [
57  "css" => $this->reactAppBuildStaticCssDirectoryPath,
58  "js" => $this->reactAppBuildStaticJsDirectoryPath,
59  ];
60 
61  foreach ($paths as $type => $path) {
62  $base = "js/app/build/static/{$type}/";
63  $chunks = $this->getAppChunckFiles($path);
64  $libraries = $this->setLibraries($libraries, $chunks, $type, $base);
65  }
66 
67  $yaml = Yaml::dump($libraries);
68  file_put_contents($this->librariesFilePath, $yaml);
69  }
70 
74  private function setLibraries($libraries, $chunks, $type, $base) {
75  foreach ($chunks as $chunk) {
76  if ($type == 'js') {
77  $libraries['dkan_metadata_form']['js'][$base . $chunk] = [];
78  }
79  else {
80  $libraries['dkan_metadata_form']['css']['base'][$base . $chunk] = [];
81  }
82  }
83  return $libraries;
84  }
85 
89  private function getAppChunckFiles($path) {
90  $folderInfo = scandir($path);
91  unset($folderInfo[0]);
92  unset($folderInfo[1]);
93  $chunks = [];
94  foreach ($folderInfo as $dirfile) {
95  if (!$this->skip($dirfile)) {
96  $chunks[] = $dirfile;
97  }
98  }
99  return $chunks;
100  }
101 
105  private function skip($dirfile) {
106  $skips = ["LICENSE", 'map', 'loadme', 'runtime'];
107  $skip = FALSE;
108  foreach ($skips as $s) {
109  if (substr_count($dirfile, $s) > 0) {
110  $skip = TRUE;
111  break;
112  }
113  }
114  return $skip;
115  }
116 
120  private function removeExistingFile() {
121  if (file_exists($this->librariesFilePath)) {
122  unlink($this->librariesFilePath);
123  }
124  }
125 
129  private function getLibrariesBasicStructure() {
130 
131  return [
132  'dkan_metadata_form' => [
133  "version" => "1.x",
134  "js" => [
135  "js/app/build/static/js/loadme.js" => [],
136  ],
137  "css" => [
138  "base" => [],
139  ],
140  "dependencies" => [
141  "core/drupalSettings",
142  ],
143  ],
144  ];
145  }
146 
150  private function createLoadMeJs() {
151  $loadMeJsFilePath = $this->reactAppBuildStaticJsDirectoryPath . "/loadme.js";
152 
153  if (file_exists($loadMeJsFilePath)) {
154  unlink($loadMeJsFilePath);
155  }
156 
157  $indexFilePath = $this->reactAppBuildDirectoryPath . "/index.html";
158  $input = new FileInputStream($indexFilePath);
159 
160  $html = new HTML5();
161  $document = $html->parse($input);
162  $scriptTags = $document->getElementsByTagName("script");
163 
164  /* @var $scriptTag DOMElement */
165  foreach ($scriptTags as $scriptTag) {
166  $content = $scriptTag->textContent;
167  if (!empty($content)) {
168  file_put_contents($loadMeJsFilePath, $content);
169  }
170  }
171  }
172 
173 }
Drupal\dkan_metadata_form
Definition: Controller.php:3
Drupal\dkan_metadata_form\Drush\sync
sync()
Definition: Drush.php:43
Drupal\dkan_metadata_form\Drush\__construct
__construct()
Definition: Drush.php:27
Drupal\dkan_metadata_form\Drush
Definition: Drush.php:15