wp-optimize
/
vendor
/
robloach
/
component-installer
/
src
/
ComponentInstaller
/
Process
/
RequireCssProcess.php
RequireCssProcess.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.3, at vendor/robloach/component-installer/src/ComponentInstaller/Process/RequireCssProcess.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\Config; |
| 16 | use Composer\Package\Package; |
| 17 | use Assetic\Asset\AssetCollection; |
| 18 | use Assetic\Filter\CssRewriteFilter; |
| 19 | use Assetic\Asset\FileAsset; |
| 20 | use Assetic\Filter\FilterCollection; |
| 21 | |
| 22 | /** |
| 23 | * Builds the require.css file from all Component stylesheets. |
| 24 | */ |
| 25 | class RequireCssProcess extends Process |
| 26 | { |
| 27 | /** |
| 28 | * {@inheritdoc} |
| 29 | */ |
| 30 | public function process() |
| 31 | { |
| 32 | $filters = new FilterCollection(array( |
| 33 | new CssRewriteFilter(), |
| 34 | )); |
| 35 | $assets = new AssetCollection(); |
| 36 | $styles = $this->packageStyles($this->packages); |
| 37 | foreach ($styles as $package => $packageStyles) { |
| 38 | foreach ($packageStyles as $style => $paths) { |
| 39 | foreach ($paths as $path) { |
| 40 | // The full path to the CSS file. |
| 41 | $assetPath = realpath($path); |
| 42 | // The root of the CSS file. |
| 43 | $sourceRoot = dirname($path); |
| 44 | // The style path to the CSS file when external. |
| 45 | $sourcePath = $package . '/' . $style; |
| 46 | // Where the final CSS will be generated. |
| 47 | $targetPath = $this->componentDir; |
| 48 | // Build the asset and add it to the collection. |
| 49 | $asset = new FileAsset($assetPath, $filters, $sourceRoot, $sourcePath); |
| 50 | $asset->setTargetPath($targetPath); |
| 51 | $assets->add($asset); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | $css = $assets->dump(); |
| 57 | if (file_put_contents($this->componentDir . '/require.css', $css) === FALSE) { |
| 58 | $this->io->write('<error>Error writing require.css to destination</error>'); |
| 59 | |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Retrieves an array of styles from a collection of packages. |
| 66 | * |
| 67 | * @param $packages |
| 68 | * An array of packages from the composer.lock file. |
| 69 | * @param $config |
| 70 | * The Composer Config object. |
| 71 | * |
| 72 | * @return array |
| 73 | * A set of package styles. |
| 74 | */ |
| 75 | public function packageStyles(array $packages) |
| 76 | { |
| 77 | $output = array(); |
| 78 | |
| 79 | // Construct the packages configuration. |
| 80 | foreach ($packages as $package) { |
| 81 | // Retrieve information from the extra options. |
| 82 | $extra = isset($package['extra']) ? $package['extra'] : array(); |
| 83 | $name = $this->getComponentName($package['name'], $extra); |
| 84 | $component = isset($extra['component']) ? $extra['component'] : array(); |
| 85 | $styles = isset($component['styles']) ? $component['styles'] : array(); |
| 86 | $vendorDir = $this->getVendorDir($package); |
| 87 | |
| 88 | // Loop through each style. |
| 89 | foreach ($styles as $style) { |
| 90 | // Find the style path from the vendor directory. |
| 91 | $path = $vendorDir.'/'.$style; |
| 92 | |
| 93 | // Search for the candidate with a glob recursive file search. |
| 94 | $files = $this->fs->recursiveGlobFiles($path); |
| 95 | foreach ($files as $file) { |
| 96 | // Provide the package name, style and full path. |
| 97 | $output[$name][$style][] = $file; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return $output; |
| 103 | } |
| 104 | } |
| 105 |