wp-optimize
/
vendor
/
robloach
/
component-installer
/
src
/
ComponentInstaller
/
Process
/
BuildJsProcess.php
BuildJsProcess.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.5, at vendor/robloach/component-installer/src/ComponentInstaller/Process/BuildJsProcess.php
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of Component Installer. |
| 5 | * |
| 6 | * (c) Rob Loach (http://robloach.net) |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE.md |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace ComponentInstaller\Process; |
| 13 | |
| 14 | use Composer\Composer; |
| 15 | use Composer\Package\Package; |
| 16 | use Composer\Package\Loader\ArrayLoader; |
| 17 | use Composer\Util\Filesystem; |
| 18 | |
| 19 | /** |
| 20 | * Builds all JavaScript Components into one require-built.js. |
| 21 | */ |
| 22 | class BuildJsProcess extends Process |
| 23 | { |
| 24 | /** |
| 25 | * {@inheritdoc} |
| 26 | */ |
| 27 | public function process() |
| 28 | { |
| 29 | return $this->compile($this->packages); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Copy file assets from the given packages to the component directory. |
| 34 | * |
| 35 | * @param array $packages |
| 36 | * An array of packages. |
| 37 | */ |
| 38 | public function compile($packages) |
| 39 | { |
| 40 | // Set up the initial require-build.js file. |
| 41 | $destination = $this->componentDir.DIRECTORY_SEPARATOR.'require-built.js'; |
| 42 | $require = $this->componentDir.DIRECTORY_SEPARATOR.'require.js'; |
| 43 | copy($require, $destination); |
| 44 | |
| 45 | // Cycle through each package and add it to the built require.js file. |
| 46 | foreach ($packages as $package) { |
| 47 | // Retrieve some information about the package |
| 48 | $name = isset($package['name']) ? $package['name'] : '__component__'; |
| 49 | $extra = isset($package['extra']) ? $package['extra'] : array(); |
| 50 | $componentName = $this->getComponentName($name, $extra); |
| 51 | |
| 52 | // Find where the source file is located. |
| 53 | $packageDir = $this->componentDir.DIRECTORY_SEPARATOR.$componentName; |
| 54 | $source = $packageDir.DIRECTORY_SEPARATOR.$componentName.'-built.js'; |
| 55 | |
| 56 | // Make sure the source script is available. |
| 57 | if (file_exists($source)) { |
| 58 | // Build the compiled script. |
| 59 | $content = file_get_contents($source); |
| 60 | $output = $this->definePrefix($componentName) . $content . $this->definePostfix(); |
| 61 | |
| 62 | // Append the module definition to the destination file. |
| 63 | file_put_contents($destination, $output, FILE_APPEND); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Provide the initial definition prefix. |
| 72 | * |
| 73 | * @return string Begin the module definition. |
| 74 | */ |
| 75 | protected function definePrefix($componentName) |
| 76 | { |
| 77 | // Define the module using the simplified CommonJS wrapper. |
| 78 | return "\ndefine('$componentName', function (require, exports, module) {\n"; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Finish the module definition. |
| 83 | * |
| 84 | * @return string Close brackets to finish the module. |
| 85 | */ |
| 86 | protected function definePostfix() |
| 87 | { |
| 88 | return "\n});\n"; |
| 89 | } |
| 90 | } |
| 91 |