-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Project.php
141 lines (119 loc) · 3.31 KB
/
Project.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace Couscous\Model;
use Couscous\Model\WatchList\WatchList;
use Symfony\Component\Finder\Finder;
/**
* Project containing files.
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class Project
{
/**
* Directory containing the sources files to process.
*
* @var string
*/
public $sourceDirectory;
/**
* Directory in which to generate the website.
*
* @var string
*/
public $targetDirectory;
/**
* @var Metadata
*/
public $metadata;
/**
* If true then we are in "preview" mode and we are regenerating the website.
*
* Use this to prevent doing anything costly twice so that the regeneration is faster,
* e.g. downloading files…
*
* @var bool
*/
public $regenerate = false;
/**
* @var WatchList
*/
public $watchlist;
/**
* Files that will be written to disk at the end of the generation.
*
* @var File[]
*/
protected $files = [];
public function __construct($sourceDirectory, $targetDirectory)
{
$this->sourceDirectory = $sourceDirectory;
$this->targetDirectory = $targetDirectory;
$this->watchlist = new WatchList();
$this->metadata = new Metadata();
}
public function addFile(File $file)
{
$this->files[$file->relativeFilename] = $file;
}
public function removeFile(File $file)
{
unset($this->files[$file->relativeFilename]);
}
public function replaceFile(File $oldFile, File $newFile)
{
$this->removeFile($oldFile);
$this->addFile($newFile);
}
/**
* @return File[]
*/
public function getFiles()
{
return $this->files;
}
/**
* @param string $class
*
* @return File[] Instances of $class
*/
public function findFilesByType($class)
{
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf(
"The class %s doesn't exist",
$class
));
}
return array_filter($this->files, function (File $file) use ($class) {
return $file instanceof $class;
});
}
/**
* Returns a Finder correctly set up for searching in source files.
*
* @return Finder
*/
public function sourceFiles()
{
$includedDirectories = $this->metadata['include'] ? $this->metadata['include'] : [];
// To be sure that included directories are under the source one
if (!empty($includedDirectories)) {
array_walk($includedDirectories, function (&$item) {
$item = $this->sourceDirectory.'/'.$item;
});
}
$excludedDirectories = new ExcludeList($this->metadata['exclude'] ? $this->metadata['exclude'] : []);
if (is_file($this->sourceDirectory.'/.gitignore')) {
$excludedDirectories->addEntries(file($this->sourceDirectory.'/.gitignore'));
}
$finder = new Finder();
$finder->files()
->followLinks()
->in(!empty($includedDirectories) ? $includedDirectories : $this->sourceDirectory)
->ignoreDotFiles(true);
$excludedDirectories
->addEntry('.couscous')
->excludeFromFinder($finder);
return $finder;
}
}