1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Repository; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Finder\Finder; |
6
|
|
|
use AppBundle\Entity\Psr; |
7
|
|
|
use Mni\FrontYAML\Parser; |
8
|
|
|
|
9
|
|
|
class PsrRepository |
10
|
|
|
{ |
11
|
|
|
public function __construct($kernelRootDir) |
12
|
|
|
{ |
13
|
|
|
$this->path = $kernelRootDir; |
|
|
|
|
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* return array |
18
|
|
|
*/ |
19
|
|
|
public function findAll() |
20
|
|
|
{ |
21
|
|
|
$finder = new Finder(); |
22
|
|
|
$finder->files()->in($this->path.'/../vendor/symfony-si/fig-standards-sl/accepted'); |
23
|
|
|
$finder->files()->name('*md'); |
24
|
|
|
$finder->sortByName(); |
25
|
|
|
|
26
|
|
|
$psrs = []; |
27
|
|
|
foreach ($finder as $file) { |
28
|
|
|
if (substr($file->getFileName(), -7) != 'meta.md' && substr($file->getFileName(), -11) != 'examples.md') { |
29
|
|
|
$psrs[] = $this->getPsrByFile($file->getRealPath()); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $psrs; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $slug |
38
|
|
|
* @return Psr |
39
|
|
|
*/ |
40
|
|
|
public function findOneBySlug($slug) |
41
|
|
|
{ |
42
|
|
|
$finder = new Finder(); |
43
|
|
|
$finder->files()->in($this->path.'/../vendor/symfony-si/fig-standards-sl/accepted')->name("*.md"); |
44
|
|
|
$finder->sortByName(); |
45
|
|
|
|
46
|
|
|
$parser = new Parser(); |
47
|
|
|
foreach ($finder as $file) { |
48
|
|
|
$document = $parser->parse($file->getContents()); |
49
|
|
|
if ($document->getYAML()['slug'] == $slug) { |
50
|
|
|
return $this->getPsrByFile($file->getRealPath()); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $file |
59
|
|
|
* @return null|Psr |
60
|
|
|
*/ |
61
|
|
|
public function getPsrByFile($file) |
62
|
|
|
{ |
63
|
|
|
if (!file_exists($file)) { |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$psr = $this->getPsrByFileWithoutChilds($file); |
68
|
|
|
$metaFile = substr($file, 0, -3).'-meta.md'; |
69
|
|
|
if (file_exists($metaFile)) { |
70
|
|
|
$meta = $this->getPsrByFileWithoutChilds($metaFile); |
71
|
|
|
$psr->setMeta($meta); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$examplesFile = substr($file, 0, -3).'-examples.md'; |
75
|
|
|
if (file_exists($examplesFile)) { |
76
|
|
|
$examples = $this->getPsrByFileWithoutChilds($examplesFile); |
77
|
|
|
$psr->setExamples($examples); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $psr; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get Psr without meta or example child documents. |
85
|
|
|
* |
86
|
|
|
* @param string $file |
87
|
|
|
* @return Psr |
88
|
|
|
*/ |
89
|
|
|
private function getPsrByFileWithoutChilds($file) |
90
|
|
|
{ |
91
|
|
|
$parser = new Parser(); |
92
|
|
|
$document = $parser->parse(file_get_contents($file)); |
93
|
|
|
|
94
|
|
|
$psr = new Psr(); |
95
|
|
|
$psr->setTitle($document->getYAML()['title']); |
96
|
|
|
$psr->setSlug($document->getYaml()['slug']); |
97
|
|
|
$psr->setContent($document->getContent()); |
98
|
|
|
$psr->setReadTime($document->getYAML()['read_time']); |
99
|
|
|
$psr->setDescription($document->getYAML()['description']); |
100
|
|
|
$psr->setUpdated(\DateTime::createFromFormat('Y-m-d', $document->getYAML()['updated'])); |
|
|
|
|
101
|
|
|
|
102
|
|
|
return $psr; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: