private function combine(string $realPath): array
  {
    $config = $this->extractConfigFromMainFile(self::getMainJsFileName($realPath));

    // Create temporary file with config.
    $tmp_name1 = tempnam('.', 'abc_');
    $handle    = fopen($tmp_name1, 'w');
    fwrite($handle, $config);
    fclose($handle);

    // Create temporary file for combined JavaScript code.
    $tmp_name2 = tempnam($this->resourceDirFullPath, 'abc_');

    // Run r.js.
    $command = [$this->combineCommand,
                '-o',
                $tmp_name1,
                'baseUrl='.$this->resourceDirFullPath,
                'optimize=none',
                'name='.$this->getNamespaceFromResourceFilename($realPath),
                'out='.$tmp_name2];
    $output  = $this->execCommand($command);

    // Get all files of the combined code.
    $parts   = [];
    $trigger = array_search('----------------', $output);
    foreach ($output as $index => $file)
    {
      if ($index>$trigger && !empty($file))
      {
        $parts[] = $file;
      }
    }

    // Get the combined the JavaScript code.
    $code = file_get_contents($tmp_name2);
    if ($code===false) $this->logError("Unable to read file '%s'.", $tmp_name2);

    // Get require.js
    $path       = $this->parentResourceDirFullPath.'/'.$this->requireJsPath;
    $require_js = file_get_contents($path);
    if ($code===false) $this->logError("Unable to read file '%s'.", $path);

    // Combine require.js and all required includes.
    $code = $require_js.$code;

    // Remove temporary files.
    unlink($tmp_name2);
    unlink($tmp_name1);

    return ['code' => $code, 'parts' => $parts];
  }
private function combine(string $realPath): array
  {
    $config = $this->extractConfigFromMainFile(self::getMainJsFileName($realPath));

    // Create temporary file with config.
    $tmp_name1 = tempnam('.', 'abc_');
    $handle    = fopen($tmp_name1, 'w');
    fwrite($handle, $config);
    fclose($handle);

    // Create temporary file for combined JavaScript code.
    $tmp_name2 = tempnam($this->resourceDirFullPath, 'abc_');

    // Run r.js.
    $command = [$this->combineCommand,
                '-o',
                $tmp_name1,
                'baseUrl='.$this->resourceDirFullPath,
                'optimize=none',
                'name='.$this->getNamespaceFromResourceFilename($realPath),
                'out='.$tmp_name2];
    $output  = $this->execCommand($command);

    // Get all files of the combined code.
    $parts   = [];
    $trigger = array_search('----------------', $output);
    foreach ($output as $index => $file)
    {
      if ($index>$trigger && !empty($file))
      {
        $parts[] = $file;
      }
    }

    // Get the combined the JavaScript code.
    $code = file_get_contents($tmp_name2);
    if ($code===false) $this->logError("Unable to read file '%s'.", $tmp_name2);

    // Get require.js
    $path       = $this->parentResourceDirFullPath.'/'.$this->requireJsPath;
    $require_js = file_get_contents($path);
    if ($code===false) $this->logError("Unable to read file '%s'.", $path);

    // Combine require.js and all required includes.
    $code = $require_js.$code;

    // Remove temporary files.
    unlink($tmp_name2);
    unlink($tmp_name1);

    return ['code' => $code, 'parts' => $parts];
  }
private function combine ( string $ realPath ) : array { $ config = $ this -> extractConfigFromMainFile ( self :: getMainJsFileName ( $ realPath ) ) ; // Create temporary file with config. $ tmp_name1 = tempnam ( '.' , 'abc_' ) ; $ handle = fopen ( $ tmp_name1 , 'w' ) ; fwrite ( $ handle , $ config ) ; fclose ( $ handle ) ; // Create temporary file for combined JavaScript code. $ tmp_name2 = tempnam ( $ this -> resourceDirFullPath , 'abc_' ) ; // Run r.js. $ command = [ $ this -> combineCommand , '-o' , $ tmp_name1 , 'baseUrl=' . $ this -> resourceDirFullPath , 'optimize=none' , 'name=' . $ this -> getNamespaceFromResourceFilename ( $ realPath ) , 'out=' . $ tmp_name2 ] ; $ output = $ this -> execCommand ( $ command ) ; // Get all files of the combined code. $ parts = [ ] ; $ trigger = array_search ( '----------------' , $ output ) ; foreach ( $ output as $ index => $ file ) { if ( $ index > $ trigger && ! empty ( $ file ) ) { $ parts [ ] = $ file ; } } // Get the combined the JavaScript code. $ code = file_get_contents ( $ tmp_name2 ) ; if ( $ code === false ) $ this -> logError ( "Unable to read file '%s'." , $ tmp_name2 ) ; // Get require.js $ path = $ this -> parentResourceDirFullPath . '/' . $ this -> requireJsPath ; $ require_js = file_get_contents ( $ path ) ; if ( $ code === false ) $ this -> logError ( "Unable to read file '%s'." , $ path ) ; // Combine require.js and all required includes. $ code = $ require_js . $ code ; // Remove temporary files. unlink ( $ tmp_name2 ) ; unlink ( $ tmp_name1 ) ; return [ 'code' => $ code , 'parts' => $ parts ] ; }
Combines all JavaScript files required by a main JavaScript file.

@param string $realPath The path to the main JavaScript file.

@return array The combined code and parts.
Combines all JavaScript files required by a main JavaScript file .