1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is a part of Sculpin. |
5
|
|
|
* |
6
|
|
|
* (c) Dragonfly Development Inc. |
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
|
|
|
namespace Symplify\PHP7_Sculpin\Source; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
15
|
|
|
use Dflydev\Canal\Analyzer\Analyzer; |
16
|
|
|
use Dflydev\DotAccessConfiguration\Configuration as Data; |
17
|
|
|
use Dflydev\DotAccessConfiguration\YamlConfigurationBuilder as YamlDataBuilder; |
18
|
|
|
|
19
|
|
|
final class FileSource extends AbstractSource |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
protected function init(bool $hasChanged = null) |
24
|
|
|
{ |
25
|
|
|
$content = file_get_contents($this->file); |
26
|
|
|
|
27
|
|
|
// extract data from file! |
28
|
|
|
if (preg_match('/^\s*(?:---[\s]*[\r\n]+)(.*?)(?:---[\s]*[\r\n]+)(.*?)$/s', $content, $matches)) { |
29
|
|
|
$this->content = $matches[2]; |
30
|
|
|
if (preg_match('/^(\s*[-]+\s*|\s*)$/', $matches[1])) { |
31
|
6 |
|
// There is nothing useful in the YAML front matter. |
32
|
|
|
$this->data = new Data(); |
33
|
|
|
} else { |
34
|
|
|
// There may be YAML frontmatter |
35
|
|
|
try { |
36
|
|
|
$builder = new YamlDataBuilder($matches[1]); |
37
|
|
|
$this->data = $builder->build(); |
38
|
6 |
|
} catch (\InvalidArgumentException $e) { |
39
|
6 |
|
// Likely not actually YAML front matter available, |
40
|
6 |
|
// treat the entire file as pure content. |
41
|
6 |
|
echo ' ! '.$this->sourceId().' '.$e->getMessage().' !'.PHP_EOL; |
42
|
6 |
|
$this->content = $content; |
43
|
6 |
|
$this->data = new Data(); |
44
|
6 |
|
} |
45
|
|
|
} |
46
|
6 |
|
} else { |
47
|
6 |
|
$this->content = $content; |
48
|
|
|
$this->data = new Data(); |
49
|
6 |
|
$this->canBeFormatted = false; |
50
|
6 |
|
} |
51
|
|
|
} |
52
|
|
|
|
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.