1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of Cecil. |
||
5 | * |
||
6 | * (c) Arnaud Ligny <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Cecil\Step\StaticFiles; |
||
15 | |||
16 | use Cecil\Exception\RuntimeException; |
||
17 | use Cecil\Step\AbstractStep; |
||
18 | use Cecil\Util; |
||
19 | use Symfony\Component\Finder\Finder; |
||
20 | |||
21 | /** |
||
22 | * Copy static files step. |
||
23 | * |
||
24 | * This step is responsible for copying static files from the source directories |
||
25 | * (like `static/` and `assets/`) to the output directory. It handles both files |
||
26 | * and directories, allowing for exclusions based on the configuration. It also |
||
27 | * supports copying files from theme-specific directories if themes are configured. |
||
28 | * The step can be run in a dry-run mode where no actual file operations are performed, |
||
29 | * but the intended actions are still logged. |
||
30 | */ |
||
31 | class Copy extends AbstractStep |
||
32 | { |
||
33 | protected $count = 0; |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | */ |
||
38 | 1 | public function getName(): string |
|
39 | { |
||
40 | 1 | return 'Copying static'; |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 1 | public function init(array $options): void |
|
47 | { |
||
48 | 1 | if ($options['dry-run']) { |
|
49 | return; |
||
50 | } |
||
51 | |||
52 | // reset output directory only if it's not partial rendering |
||
53 | 1 | if (empty($options['render-subset'])) { |
|
54 | 1 | Util\File::getFS()->remove($this->config->getOutputPath()); |
|
55 | 1 | Util\File::getFS()->mkdir($this->config->getOutputPath()); |
|
56 | } |
||
57 | |||
58 | 1 | $this->canProcess = true; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | 1 | public function process(): void |
|
65 | { |
||
66 | 1 | $target = (string) $this->config->get('static.target'); |
|
67 | 1 | $exclude = (array) $this->config->get('static.exclude'); |
|
68 | |||
69 | // copying assets in debug mode (for source maps) |
||
70 | 1 | if ($this->builder->isDebug() && $this->config->isEnabled('assets.compile.sourcemap')) { |
|
71 | // copying content of '<theme>/assets/' dir if exists |
||
72 | if ($this->config->hasTheme()) { |
||
73 | $themes = array_reverse($this->config->getTheme()); |
||
74 | foreach ($themes as $theme) { |
||
75 | $this->copy($this->config->getThemeDirPath($theme, 'assets')); |
||
76 | } |
||
77 | } |
||
78 | // copying content of 'assets/' dir if exists |
||
79 | $this->copy($this->config->getAssetsPath()); |
||
80 | // cancel exclusion for static files (see below) |
||
81 | $exclude = []; |
||
82 | } |
||
83 | |||
84 | // copying content of '<theme>/static/' dir if exists |
||
85 | 1 | if ($this->config->hasTheme()) { |
|
86 | 1 | $themes = array_reverse($this->config->getTheme()); |
|
87 | 1 | foreach ($themes as $theme) { |
|
88 | 1 | $this->copy($this->config->getThemeDirPath($theme, 'static'), $target, $exclude); |
|
89 | } |
||
90 | } |
||
91 | |||
92 | // copying content of 'static/' dir if exists |
||
93 | 1 | $this->copy($this->config->getStaticPath(), $target, $exclude); |
|
94 | |||
95 | // copying mounts |
||
96 | 1 | if ($this->config->get('static.mounts')) { |
|
97 | 1 | foreach ((array) $this->config->get('static.mounts') as $source => $destination) { |
|
98 | 1 | $this->copy(Util::joinFile($this->config->getStaticPath(), (string) $source), (string) $destination); |
|
99 | } |
||
100 | } |
||
101 | |||
102 | 1 | if ($this->count === 0) { |
|
103 | $this->builder->getLogger()->info('Nothing to copy'); |
||
104 | |||
105 | return; |
||
106 | } |
||
107 | 1 | $this->builder->getLogger()->info('Files copied', ['progress' => [$this->count, $this->count]]); |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Copying a file or files in a directory from $from (if exists) to $to (relative to output path). |
||
112 | * Exclude files or directories with $exclude array. |
||
113 | */ |
||
114 | 1 | protected function copy(string $from, ?string $to = null, ?array $exclude = null): void |
|
115 | { |
||
116 | try { |
||
117 | 1 | if (Util\File::getFS()->exists($from)) { |
|
118 | // copy a file |
||
119 | 1 | if (is_file($from)) { |
|
120 | 1 | Util\File::getFS()->copy($from, Util::joinFile($this->config->getOutputPath(), $to), true); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
121 | |||
122 | 1 | return; |
|
123 | } |
||
124 | // copy a directory |
||
125 | 1 | $finder = Finder::create() |
|
126 | 1 | ->files() |
|
127 | 1 | ->in($from) |
|
128 | 1 | ->ignoreDotFiles(false); |
|
129 | // exclude files or directories |
||
130 | 1 | if (\is_array($exclude)) { |
|
131 | 1 | $finder->notPath($exclude); |
|
132 | 1 | $finder->notName($exclude); |
|
133 | } |
||
134 | 1 | $this->count += $finder->count(); |
|
135 | 1 | Util\File::getFS()->mirror( |
|
136 | 1 | $from, |
|
137 | 1 | Util::joinFile($this->config->getOutputPath(), $to ?? ''), |
|
138 | 1 | $finder, |
|
139 | 1 | ['override' => true] |
|
140 | 1 | ); |
|
141 | } |
||
142 | } catch (\Exception $e) { |
||
143 | throw new RuntimeException(\sprintf('Error during static files copy: %s', $e->getMessage())); |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 |