Completed
Push — master ( dab7b5...3dae04 )
by Vladimir
02:49
created

AssetManager::createNewItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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