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
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $path; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Parser |
18
|
|
|
*/ |
19
|
|
|
private $parser; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* PsrRepository constructor. |
23
|
|
|
* |
24
|
|
|
* @param $kernelProjectDir |
25
|
|
|
* @param Parser $parser |
26
|
|
|
*/ |
27
|
|
|
public function __construct($kernelProjectDir, Parser $parser) |
28
|
|
|
{ |
29
|
|
|
$this->path = $kernelProjectDir; |
30
|
|
|
$this->parser = $parser; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* return array |
35
|
|
|
*/ |
36
|
|
|
public function findAll() |
37
|
|
|
{ |
38
|
|
|
$finder = new Finder(); |
39
|
|
|
$finder->files()->in($this->path.'/vendor/symfony-si/fig-standards-sl/accepted'); |
40
|
|
|
$finder->files()->name('*md'); |
41
|
|
|
$finder->sortByName(); |
42
|
|
|
|
43
|
|
|
$psrs = []; |
44
|
|
|
foreach ($finder as $file) { |
45
|
|
|
if (substr($file->getFileName(), -7) != 'meta.md' && substr($file->getFileName(), -11) != 'examples.md') { |
46
|
|
|
$psrs[] = $this->getPsrByFile($file->getRealPath()); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $psrs; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $slug |
55
|
|
|
* @return Psr |
56
|
|
|
*/ |
57
|
|
|
public function findOneBySlug($slug) |
58
|
|
|
{ |
59
|
|
|
$finder = new Finder(); |
60
|
|
|
$finder->files()->in($this->path.'/vendor/symfony-si/fig-standards-sl/accepted')->name("*.md"); |
61
|
|
|
$finder->sortByName(); |
62
|
|
|
|
63
|
|
|
foreach ($finder as $file) { |
64
|
|
|
$document = $this->parser->parse($file->getContents()); |
65
|
|
|
if ($document->getYAML()['slug'] == $slug) { |
66
|
|
|
return $this->getPsrByFile($file->getRealPath()); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $file |
75
|
|
|
* @return null|Psr |
76
|
|
|
*/ |
77
|
|
|
public function getPsrByFile($file) |
78
|
|
|
{ |
79
|
|
|
if (!file_exists($file)) { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$psr = $this->getPsrByFileWithoutChilds($file); |
84
|
|
|
$metaFile = substr($file, 0, -3).'-meta.md'; |
85
|
|
|
if (file_exists($metaFile)) { |
86
|
|
|
$meta = $this->getPsrByFileWithoutChilds($metaFile); |
87
|
|
|
$psr->setMeta($meta); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$examplesFile = substr($file, 0, -3).'-examples.md'; |
91
|
|
|
if (file_exists($examplesFile)) { |
92
|
|
|
$examples = $this->getPsrByFileWithoutChilds($examplesFile); |
93
|
|
|
$psr->setExamples($examples); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $psr; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get Psr without meta or example child documents. |
101
|
|
|
* |
102
|
|
|
* @param string $file |
103
|
|
|
* @return Psr |
104
|
|
|
*/ |
105
|
|
|
private function getPsrByFileWithoutChilds($file) |
106
|
|
|
{ |
107
|
|
|
$document = $this->parser->parse(file_get_contents($file)); |
108
|
|
|
|
109
|
|
|
$psr = new Psr(); |
110
|
|
|
$psr->setTitle($document->getYAML()['title']); |
111
|
|
|
$psr->setSlug($document->getYaml()['slug']); |
112
|
|
|
$psr->setContent($document->getContent()); |
113
|
|
|
$psr->setReadTime($document->getYAML()['read_time']); |
114
|
|
|
$psr->setDescription($document->getYAML()['description']); |
115
|
|
|
$psr->setUpdated(\DateTime::createFromFormat('Y-m-d', $document->getYAML()['updated'])); |
|
|
|
|
116
|
|
|
|
117
|
|
|
return $psr; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|