|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace allejo\stakx\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use allejo\stakx\System\Folder; |
|
6
|
|
|
use Symfony\Component\Finder\Finder; |
|
7
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
8
|
|
|
|
|
9
|
|
|
abstract class FileManager extends BaseManager |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var Folder |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $outputDirectory; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Whether or not to enable tracking of files to keep a copy to be able to update them. This should only be set to |
|
18
|
|
|
* True during the watch command. |
|
19
|
|
|
* |
|
20
|
|
|
* @var bool |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $tracking; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Finder |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $finder; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var SplFileInfo[] |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $files; |
|
33
|
|
|
|
|
34
|
|
|
public function setTracking ($enabled) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->tracking = $enabled; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function isFileAsset ($filePath) |
|
40
|
|
|
{ |
|
41
|
|
|
return (array_key_exists($filePath, $this->files)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function copyFile ($filePath) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->output->notice('Copying static asset: {file}', array('file' => $filePath)); |
|
47
|
|
|
|
|
48
|
|
|
$this->copyToCompiledSite($this->files[$filePath]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param Folder $directory |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setFolder ($directory) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->outputDirectory = $directory; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param SplFileInfo $file The relative path of the file to be copied |
|
61
|
|
|
* @param string $prefix |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function copyToCompiledSite ($file, $prefix = "") |
|
64
|
|
|
{ |
|
65
|
|
|
if (!$this->fs->exists($file)) { return; } |
|
66
|
|
|
|
|
67
|
|
|
$filePath = $file->getRealPath(); |
|
68
|
|
|
$pathToStrip = $this->fs->appendPath(getcwd(), $prefix); |
|
69
|
|
|
$siteTargetPath = ltrim(str_replace($pathToStrip, "", $filePath), DIRECTORY_SEPARATOR); |
|
70
|
|
|
|
|
71
|
|
|
try |
|
72
|
|
|
{ |
|
73
|
|
|
$this->outputDirectory->copyFile($filePath, $siteTargetPath); |
|
74
|
|
|
} |
|
75
|
|
|
catch (\Exception $e) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->output->error($e->getMessage()); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
abstract public function copyFiles (); |
|
82
|
|
|
} |