ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ComponentInstaller\Process\RequireJsProcess Class Reference

Builds the require.js configuration. More...

+ Inheritance diagram for ComponentInstaller\Process\RequireJsProcess:
+ Collaboration diagram for ComponentInstaller\Process\RequireJsProcess:

Public Member Functions

 init ()
 {Initialize the process before its run.
Returns
boolean Whether or not the process should continue after initialization.
} More...
 
 process ()
 {{Called when running through the process.
Returns
boolean True or false depending on whether the process was successful.
}} More...
 
 requireJson (array $packages)
 Creates a require.js configuration from an array of packages. More...
 
 aggregateScripts ($package, array $scripts, $file)
 Concatenate all scripts together into one destination file. More...
 
 requireJs (array $json=array())
 Constructs the require.js file from the provided require.js JSON array. More...
 
- Public Member Functions inherited from ComponentInstaller\Process\Process
 __construct (Composer $composer=null, IOInterface $io=null)
 {Create a new Process.
Parameters
Composer$composerThe Composer object to act on.
IOInterface$ioInput/Output object to act on.
} More...
 
 process ()
 {Called when running through the process.
Returns
boolean True or false depending on whether the process was successful.
} More...
 
 getComponentName ($prettyName, array $extra=array())
 Retrieves the component name for the component. More...
 
 getComponentDir ()
 Retrieves the component directory. More...
 
 setComponentDir ($dir)
 Sets the component directory. More...
 
 getVendorDir (array $package)
 Retrieves the given package's vendor directory, where it's installed. More...
 
 __construct (Composer $composer, IOInterface $io)
 Create a new Process. More...
 
 init ()
 Initialize the process before its run. More...
 
 process ()
 Called when running through the process. More...
 

Protected Member Functions

 arrayMergeRecursiveDistinct (array &$array1, array &$array2)
 Merges two arrays without changing string array keys. More...
 

Protected Attributes

 $baseUrl = 'components'
 The base URL for the require.js configuration. More...
 
- Protected Attributes inherited from ComponentInstaller\Process\Process
 $composer
 
 $io
 
 $config
 
 $packages = array()
 
 $componentDir = 'components'
 
 $fs
 
 $installationManager
 

Detailed Description

Builds the require.js configuration.

Definition at line 23 of file RequireJsProcess.php.

Member Function Documentation

◆ aggregateScripts()

ComponentInstaller\Process\RequireJsProcess::aggregateScripts (   $package,
array  $scripts,
  $file 
)

Concatenate all scripts together into one destination file.

Parameters
array$package
array$scripts
string$file
Returns
bool

Definition at line 158 of file RequireJsProcess.php.

159 {
160 $assets = $this->newAssetCollection();
161
162 foreach ($scripts as $script) {
163 // Collect each candidate from a glob file search.
164 $path = $this->getVendorDir($package).DIRECTORY_SEPARATOR.$script;
165 $matches = $this->fs->recursiveGlobFiles($path);
166 foreach ($matches as $match) {
167 $assets->add(new FileAsset($match));
168 }
169 }
170 $js = $assets->dump();
171
172 // Write the file if there are any JavaScript assets.
173 if (!empty($js)) {
174 $destination = $this->componentDir.DIRECTORY_SEPARATOR.$file;
175 $this->fs->ensureDirectoryExists(dirname($destination));
176
177 return file_put_contents($destination, $js);
178 }
179
180 return false;
181 }
$path
Definition: aliased.php:25
getVendorDir(array $package)
Retrieves the given package's vendor directory, where it's installed.
Definition: Process.php:199
$js

References $js, $path, and ComponentInstaller\Process\Process\getVendorDir().

Referenced by ComponentInstaller\Process\RequireJsProcess\requireJson().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ arrayMergeRecursiveDistinct()

ComponentInstaller\Process\RequireJsProcess::arrayMergeRecursiveDistinct ( array &  $array1,
array &  $array2 
)
protected

Merges two arrays without changing string array keys.

Appends to array if keys are numeric.

See also
array_merge()
array_merge_recursive()
Parameters
array$array1
array$array2
Returns
array

Definition at line 223 of file RequireJsProcess.php.

224 {
225 $merged = $array1;
226
227 foreach ($array2 as $key => &$value) {
228 if(is_numeric($key)){
229 $merged[] = $value;
230 } else {
231 if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
232 $merged[$key] = $this->arrayMergeRecursiveDistinct($merged[$key], $value);
233 }
234 else {
235 $merged[$key] = $value;
236 }
237 }
238 }
239
240 return $merged;
241 }
arrayMergeRecursiveDistinct(array &$array1, array &$array2)
Merges two arrays without changing string array keys.

References ComponentInstaller\Process\RequireJsProcess\arrayMergeRecursiveDistinct().

Referenced by ComponentInstaller\Process\RequireJsProcess\arrayMergeRecursiveDistinct(), and ComponentInstaller\Process\RequireJsProcess\requireJson().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ init()

ComponentInstaller\Process\RequireJsProcess::init ( )

{Initialize the process before its run.

Returns
boolean Whether or not the process should continue after initialization.
}

Implements ComponentInstaller\Process\ProcessInterface.

Definition at line 33 of file RequireJsProcess.php.

34 {
35 $output = parent::init();
36 if ($this->config->has('component-baseurl')) {
37 $this->baseUrl = $this->config->get('component-baseurl');
38 }
39
40 return $output;
41 }
if(!is_dir( $entity_dir)) exit("Fatal Error ([A-Za-z0-9]+)\s+" &#(? foreach( $entity_files as $file) $output

References $output.

◆ process()

ComponentInstaller\Process\RequireJsProcess::process ( )

{{Called when running through the process.

Returns
boolean True or false depending on whether the process was successful.
}}

Reimplemented from ComponentInstaller\Process\Process.

Definition at line 46 of file RequireJsProcess.php.

47 {
48 // Construct the require.js and stick it in the destination.
49 $json = $this->requireJson($this->packages, $this->config);
50 $requireConfig = $this->requireJs($json);
51
52 // Attempt to write the require.config.js file.
53 $destination = $this->componentDir . '/require.config.js';
54 $this->fs->ensureDirectoryExists(dirname($destination));
55 if (file_put_contents($destination, $requireConfig) === FALSE) {
56 $this->io->write('<error>Error writing require.config.js</error>');
57
58 return false;
59 }
60
61 // Read in require.js to prepare the final require.js.
62 if (!file_exists(dirname(__DIR__) . '/Resources/require.js')) {
63 $this->io->write('<error>Error reading in require.js</error>');
64
65 return false;
66 }
67
68 $assets = $this->newAssetCollection();
69 $assets->add(new FileAsset(dirname(__DIR__) . '/Resources/require.js'));
70 $assets->add(new StringAsset($requireConfig));
71
72 // Append the config to the require.js and write it.
73 if (file_put_contents($this->componentDir . '/require.js', $assets->dump()) === FALSE) {
74 $this->io->write('<error>Error writing require.js to the components directory</error>');
75
76 return false;
77 }
78
79 return null;
80 }
requireJson(array $packages)
Creates a require.js configuration from an array of packages.
requireJs(array $json=array())
Constructs the require.js file from the provided require.js JSON array.

References ComponentInstaller\Process\RequireJsProcess\requireJs(), and ComponentInstaller\Process\RequireJsProcess\requireJson().

+ Here is the call graph for this function:

◆ requireJs()

ComponentInstaller\Process\RequireJsProcess::requireJs ( array  $json = array())

Constructs the require.js file from the provided require.js JSON array.

Parameters
$jsonThe require.js JSON configuration.
Returns
string The RequireJS JavaScript configuration.

Definition at line 192 of file RequireJsProcess.php.

193 {
194 // Encode the array to a JSON array.
195 $js = JsonFile::encode($json);
196
197 // Construct the JavaScript output.
198 $output = <<<EOT
199var components = $js;
200if (typeof require !== "undefined" && require.config) {
201 require.config(components);
202} else {
203 var require = components;
204}
205if (typeof exports !== "undefined" && typeof module !== "undefined") {
206 module.exports = components;
207}
208EOT;
209
210 return $output;
211 }

References $js, and $output.

Referenced by ComponentInstaller\Process\RequireJsProcess\process().

+ Here is the caller graph for this function:

◆ requireJson()

ComponentInstaller\Process\RequireJsProcess::requireJson ( array  $packages)

Creates a require.js configuration from an array of packages.

Parameters
$packagesAn array of packages from the composer.lock file.
Returns
array The built JSON array.

Definition at line 91 of file RequireJsProcess.php.

92 {
93 $json = array();
94
95 // Construct the packages configuration.
96 foreach ($packages as $package) {
97 // Retrieve information from the extra options.
98 $extra = isset($package['extra']) ? $package['extra'] : array();
99 $options = isset($extra['component']) ? $extra['component'] : array();
100
101 // Construct the base details.
102 $name = $this->getComponentName($package['name'], $extra);
103 $component = array(
104 'name' => $name,
105 );
106
107 // Build the "main" directive.
108 $scripts = isset($options['scripts']) ? $options['scripts'] : array();
109 if (!empty($scripts)) {
110 // Put all scripts into a build.js file.
111 $result = $this->aggregateScripts($package, $scripts, $name.DIRECTORY_SEPARATOR.$name.'-built.js');
112 if ($result) {
113 // If the aggregation was successful, add the script to the
114 // packages array.
115 $component['main'] = $name.'-built.js';
116
117 // Add the component to the packages array.
118 $json['packages'][] = $component;
119 }
120 }
121
122 // Add the shim definition for the package.
123 $shim = isset($options['shim']) ? $options['shim'] : array();
124 if (!empty($shim)) {
125 $json['shim'][$name] = $shim;
126 }
127
128 // Add the config definition for the package.
129 $packageConfig = isset($options['config']) ? $options['config'] : array();
130 if (!empty($packageConfig)) {
131 $json['config'][$name] = $packageConfig;
132 }
133 }
134
135 // Provide the baseUrl.
136 $json['baseUrl'] = $this->baseUrl;
137
138 // Merge in configuration options from the root.
139 if ($this->config->has('component')) {
140 $config = $this->config->get('component');
141 if (isset($config) && is_array($config)) {
142 // Use a recursive, distict array merge.
143 $json = $this->arrayMergeRecursiveDistinct($json, $config);
144 }
145 }
146
147 return $json;
148 }
$result
getComponentName($prettyName, array $extra=array())
Retrieves the component name for the component.
Definition: Process.php:154
$baseUrl
The base URL for the require.js configuration.
aggregateScripts($package, array $scripts, $file)
Concatenate all scripts together into one destination file.
if(!is_array($argv)) $options

References ComponentInstaller\Process\RequireJsProcess\$baseUrl, ComponentInstaller\Process\Process\$config, $options, ComponentInstaller\Process\Process\$packages, $result, ComponentInstaller\Process\RequireJsProcess\aggregateScripts(), ComponentInstaller\Process\RequireJsProcess\arrayMergeRecursiveDistinct(), and ComponentInstaller\Process\Process\getComponentName().

Referenced by ComponentInstaller\Process\RequireJsProcess\process().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $baseUrl

ComponentInstaller\Process\RequireJsProcess::$baseUrl = 'components'
protected

The base URL for the require.js configuration.

Definition at line 28 of file RequireJsProcess.php.

Referenced by ComponentInstaller\Process\RequireJsProcess\requireJson().


The documentation for this class was generated from the following file: