Completed
Pull Request — master (#11)
by Vladimir
02:33
created

AssetManager::handleTrackableItem()   B

Complexity

Conditions 4
Paths 11

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 21
nc 11
nop 2
dl 0
loc 35
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace allejo\stakx\Manager;
4
5
use Symfony\Component\Finder\SplFileInfo;
6
7
class AssetManager extends TrackingManager
8
{
9
    /**
10
     * The location of where to write files to
11
     *
12
     * @var Folder
13
     */
14
    private $outputDirectory;
15
16
    /**
17
     * Files or patterns to exclude from copying
18
     *
19
     * @var array
20
     */
21
    private $excludes;
22
23
    /**
24
     * Files or patterns to ensure are copied regardless of excluded patterns
25
     *
26
     * @var array
27
     */
28
    private $includes;
29
30
    /**
31
     * AssetManager constructor.
32
     *
33
     * @param array $includes
34
     * @param array $excludes
35
     */
36
    public function __construct($includes = array(), $excludes = array())
37
    {
38
        parent::__construct();
39
40
        $this->excludes = $excludes;
41
        $this->includes = $includes;
42
    }
43
44
    /**
45
     * Set the target directory of where files should be written to
46
     *
47
     * @param Folder $directory
48
     */
49
    public function setFolder ($directory)
50
    {
51
        $this->outputDirectory = $directory;
52
    }
53
54
    /**
55
     * Copy all of the assets
56
     */
57
    public function copyFiles()
58
    {
59
        $this->scanTrackableItems(
60
            '.',
61
            array(
62
                'prefix' => ''
63
            ),
64
            $this->includes,
65
            $this->excludes
66
        );
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function handleTrackableItem($file, $options = array())
73
    {
74
        if (!$this->fs->exists($file)) { return; }
75
76
        if (!($file instanceof SplFileInfo))
77
        {
78
            $file = new SplFileInfo(
79
                $this->fs->absolutePath($file),
80
                $file,
81
                $this->fs->getBaseName($file)
82
            );
83
        }
84
85
        $filePath = $file->getRealPath();
86
        $pathToStrip = $this->fs->appendPath(getcwd(), $options['prefix']);
87
        $siteTargetPath = ltrim(str_replace($pathToStrip, "", $filePath), DIRECTORY_SEPARATOR);
88
89
        try
90
        {
91
            $this->addArrayToTracker(
92
                $file->getRelativePathname(),
93
                array(),
94
                $file->getRelativePathname()
95
            );
96
            $this->saveOptions($file->getRelativePathname(), $options);
97
            $this->outputDirectory->copyFile($filePath, $siteTargetPath);
98
            $this->output->info('Copying file: {file}...', array(
99
                'file' => $file->getRelativePathname()
100
            ));
101
        }
102
        catch (\Exception $e)
103
        {
104
            $this->output->error($e->getMessage());
105
        }
106
    }
107
}