Completed
Push — master ( 0ba1b3...937b8a )
by Vladimir
02:47
created

AssetManager::refreshItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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
    public function refreshItem($filePath)
70
    {
71
        return $this->handleTrackableItem($filePath, array(
72
            'prefix' => ''
73
        ));
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function isHandled($filePath)
80
    {
81
        return $this->fileExplorer->matchesPattern($filePath);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function createNewItem($filePath)
88
    {
89
        return $this->handleTrackableItem($filePath, array(
90
            'prefix' => '',
91
        ));
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    protected function handleTrackableItem($file, $options = array())
98
    {
99
        if (is_string($file))
100
        {
101
            $file = ltrim($this->fs->appendPath($options['prefix'], $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...
102
            $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...
103
        }
104
105
        if (!$this->fs->exists($file))
106
        {
107
            return;
108
        }
109
110
        $filePath = $file->getRealPath();
111
        $pathToStrip = $this->fs->appendPath(getcwd(), $options['prefix']);
112
        $siteTargetPath = ltrim(str_replace($pathToStrip, '', $filePath), DIRECTORY_SEPARATOR);
113
114
        try
115
        {
116
            $this->addArrayToTracker(
117
                $file->getRelativePathname(),
118
                array(),
119
                $file->getRelativePathname()
120
            );
121
            $this->saveTrackerOptions($file->getRelativePathname(), $options);
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