Completed
Push — master ( 89bfcf...4ab803 )
by Vladimir
02:41
created

AssetManager::configureFinder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Manager;
9
10
use allejo\stakx\System\Folder;
11
12
class AssetManager extends TrackingManager
13
{
14
    /**
15
     * The location of where to write files to.
16
     *
17
     * @var Folder
18
     */
19
    protected $outputDirectory;
20
21
    /**
22
     * Files or patterns to exclude from copying.
23
     *
24
     * @var array
25
     */
26
    protected $excludes;
27
28
    /**
29
     * Files or patterns to ensure are copied regardless of excluded patterns.
30
     *
31
     * @var array
32
     */
33
    protected $includes;
34
35
    public function configureFinder($includes = array(), $excludes = array())
36
    {
37
        $this->excludes = $excludes;
38
        $this->includes = $includes;
39
    }
40
41
    /**
42
     * Set the target directory of where files should be written to.
43
     *
44
     * @param Folder $directory
45
     */
46
    public function setFolder($directory)
47
    {
48
        $this->outputDirectory = $directory;
49
    }
50
51
    /**
52
     * Copy all of the assets.
53
     */
54
    public function copyFiles()
55
    {
56
        $this->scanTrackableItems(
57
            getcwd(),
58
            array(
59
                'prefix' => '',
60
            ),
61
            $this->includes,
62
            array_merge(
63
                array('_themes'),
64
                $this->excludes
65
            )
66
        );
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function refreshItem($filePath)
73
    {
74
        return $this->handleTrackableItem($filePath, array(
75
            'prefix' => ''
76
        ));
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function shouldBeTracked($filePath)
83
    {
84
        return $this->fileExplorer->matchesPattern($filePath);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function createNewItem($filePath)
91
    {
92
        return $this->handleTrackableItem($filePath, array(
93
            'prefix' => '',
94
        ));
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    protected function handleTrackableItem($file, array $options = array())
101
    {
102
        if (is_string($file))
103
        {
104
            $file = ltrim($this->fs->appendPath($options['prefix'], $file), DIRECTORY_SEPARATOR);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $file. This often makes code more readable.
Loading history...
105
            $file = $this->fs->createSplFileInfo($file);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $file. This often makes code more readable.
Loading history...
106
        }
107
108
        if (!$this->fs->exists($file))
109
        {
110
            return;
111
        }
112
113
        $filePath = $file->getRealPath();
114
        $pathToStrip = $this->fs->appendPath(getcwd(), $options['prefix']);
115
        $siteTargetPath = ltrim(str_replace($pathToStrip, '', $filePath), DIRECTORY_SEPARATOR);
116
117
        try
118
        {
119
            $this->addFileToTracker($file);
120
            $this->saveTrackerOptions($file->getRelativePathname(), $options);
121
122
            $this->outputDirectory->copyFile($filePath, $siteTargetPath);
123
            $this->output->info('Copying file: {file}...', array(
124
                'file' => $file->getRelativePathname(),
125
            ));
126
        }
127
        catch (\Exception $e)
128
        {
129
            $this->output->error($e->getMessage());
130
        }
131
    }
132
}
133