ComponentInstaller\Process\RequireJsProcess::requireJson PHP Method

requireJson() public method

Creates a require.js configuration from an array of packages.
public requireJson ( array $packages ) : array
$packages array An array of packages from the composer.lock file.
return array The built JSON array.
    public function requireJson(array $packages)
    {
        $json = array();
        // Construct the packages configuration.
        foreach ($packages as $package) {
            // Retrieve information from the extra options.
            $extra = isset($package['extra']) ? $package['extra'] : array();
            $options = isset($extra['component']) ? $extra['component'] : array();
            // Construct the base details.
            $name = $this->getComponentName($package['name'], $extra);
            $component = array('name' => $name);
            // Build the "main" directive.
            $scripts = isset($options['scripts']) ? $options['scripts'] : array();
            if (!empty($scripts)) {
                // Put all scripts into a build.js file.
                $result = $this->aggregateScripts($package, $scripts, $name . DIRECTORY_SEPARATOR . $name . '-built.js');
                if ($result) {
                    // If the aggregation was successful, add the script to the
                    // packages array.
                    $component['main'] = $name . '-built.js';
                    // Add the component to the packages array.
                    $json['packages'][] = $component;
                }
            }
            // Add the shim definition for the package.
            $shim = isset($options['shim']) ? $options['shim'] : array();
            if (!empty($shim)) {
                $json['shim'][$name] = $shim;
            }
            // Add the config definition for the package.
            $packageConfig = isset($options['config']) ? $options['config'] : array();
            if (!empty($packageConfig)) {
                $json['config'][$name] = $packageConfig;
            }
        }
        // Provide the baseUrl.
        $json['baseUrl'] = $this->baseUrl;
        // Merge in configuration options from the root.
        if ($this->config->has('component')) {
            $config = $this->config->get('component');
            if (isset($config) && is_array($config)) {
                // Use a recursive, distict array merge.
                $json = $this->arrayMergeRecursiveDistinct($json, $config);
            }
        }
        return $json;
    }

Usage Example

 /**
  * testRequireJson
  *
  * @dataProvider providerRequireJson
  * @param array $packages
  * @param array $config
  * @param string $expected
  */
 public function testRequireJson(array $packages, array $config, $expected = null)
 {
     $this->composer->getConfig()->merge(array('config' => $config));
     $this->process->init();
     $result = $this->process->requireJson($packages);
     $this->assertEquals($expected, $result);
 }