Skip to content

Commit 7514fb0

Browse files
committed
Use the Parser from services
1 parent 61aa211 commit 7514fb0

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

app/config/services.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ services:
44

55
app.repository.post:
66
class: AppBundle\Repository\PostRepository
7-
arguments: ['%kernel.root_dir%']
7+
arguments: ['%kernel.root_dir%', '@markdown']
88

99
app.repository.psr:
1010
class: AppBundle\Repository\PsrRepository
11-
arguments: ['%kernel.root_dir%']
11+
arguments: ['%kernel.root_dir%', '@markdown']
1212

1313
app.twig.app_extension:
1414
class: AppBundle\Twig\AppExtension

src/AppBundle/Repository/PostRepository.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,33 @@
88

99
class PostRepository
1010
{
11+
/**
12+
* @var string
13+
*/
14+
private $path;
15+
16+
/**
17+
* @var Parser
18+
*/
19+
private $parser;
20+
1121
/**
1222
* PostRepository constructor.
1323
*
1424
* @param string $kernelRootDir
25+
* @param Parser $parser
1526
*/
16-
public function __construct($kernelRootDir)
27+
public function __construct($kernelRootDir, Parser $parser)
1728
{
1829
$this->path = $kernelRootDir;
30+
$this->parser = $parser;
1931
}
2032

33+
/**
34+
* Gets all Blog posts.
35+
*
36+
* @return array
37+
*/
2138
public function findAll()
2239
{
2340
$finder = new Finder();
@@ -89,8 +106,7 @@ public function getPostByFile($file)
89106
return null;
90107
}
91108

92-
$parser = new Parser();
93-
$document = $parser->parse(file_get_contents($file));
109+
$document = $this->parser->parse(file_get_contents($file));
94110

95111
$post = new Post();
96112
$post->setTitle($document->getYaml()['title']);

src/AppBundle/Repository/PsrRepository.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,26 @@
88

99
class PsrRepository
1010
{
11-
public function __construct($kernelRootDir)
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 $kernelRootDir
25+
* @param Parser $parser
26+
*/
27+
public function __construct($kernelRootDir, Parser $parser)
1228
{
1329
$this->path = $kernelRootDir;
30+
$this->parser = $parser;
1431
}
1532

1633
/**
@@ -43,9 +60,8 @@ public function findOneBySlug($slug)
4360
$finder->files()->in($this->path.'/../vendor/symfony-si/fig-standards-sl/accepted')->name("*.md");
4461
$finder->sortByName();
4562

46-
$parser = new Parser();
4763
foreach ($finder as $file) {
48-
$document = $parser->parse($file->getContents());
64+
$document = $this->parser->parse($file->getContents());
4965
if ($document->getYAML()['slug'] == $slug) {
5066
return $this->getPsrByFile($file->getRealPath());
5167
}
@@ -88,8 +104,7 @@ public function getPsrByFile($file)
88104
*/
89105
private function getPsrByFileWithoutChilds($file)
90106
{
91-
$parser = new Parser();
92-
$document = $parser->parse(file_get_contents($file));
107+
$document = $this->parser->parse(file_get_contents($file));
93108

94109
$psr = new Psr();
95110
$psr->setTitle($document->getYAML()['title']);

0 commit comments

Comments
 (0)