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
12namespace ComponentInstaller\Process;
13
14/**
15 * Process which copies components from their source to the components folder.
16 */
17class CopyProcess extends Process
18{
19    /**
20     * {@inheritdoc}
21     */
22    public function process()
23    {
24        return $this->copy($this->packages);
25    }
26
27    /**
28     * Copy file assets from the given packages to the component directory.
29     *
30     * @param array $packages
31     *   An array of packages.
32     * @return bool
33     */
34    public function copy($packages)
35    {
36        // Iterate over each package that should be processed.
37        foreach ($packages as $package) {
38            // Retrieve some information about the package.
39            $packageDir = $this->getVendorDir($package);
40            $name = isset($package['name']) ? $package['name'] : '__component__';
41            $extra = isset($package['extra']) ? $package['extra'] : array();
42            $componentName = $this->getComponentName($name, $extra);
43
44            // Cycle through each asset type.
45            $fileType = array('scripts', 'styles', 'files');
46            foreach ($fileType as $type) {
47                // Only act on the files if they're available.
48                if (isset($extra['component'][$type]) && is_array($extra['component'][$type])) {
49                    foreach ($extra['component'][$type] as $file) {
50                        // Make sure the file itself is available.
51                        $source = $packageDir.DIRECTORY_SEPARATOR.$file;
52
53                        // Perform a recursive glob file search on the pattern.
54                        foreach ($this->fs->recursiveGlobFiles($source) as $filesource) {
55                            // Find the final destination without the package directory.
56                            $withoutPackageDir = str_replace($packageDir.DIRECTORY_SEPARATOR, '', $filesource);
57
58                            // Construct the final file destination.
59                            $destination = $this->componentDir.DIRECTORY_SEPARATOR.$componentName.DIRECTORY_SEPARATOR.$withoutPackageDir;
60
61                            // Ensure the directory is available.
62                            $this->fs->ensureDirectoryExists(dirname($destination));
63
64                            // Copy the file to its destination.
65                            copy($filesource, $destination);
66                        }
67                    }
68                }
69            }
70        }
71
72        return true;
73    }
74}
75