1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Albert221\Blog\ServiceProvider; |
4
|
|
|
|
5
|
|
|
use Albert221\Blog\Pagination\PaginatorBuilder; |
6
|
|
|
use Albert221\Blog\Repository\CategoryRepositoryInterface; |
7
|
|
|
use Albert221\Blog\Repository\PostRepositoryInterface; |
8
|
|
|
use Albert221\Blog\Repository\SettingRepositoryInterface; |
9
|
|
|
use Albert221\Blog\Repository\TagRepositoryInterface; |
10
|
|
|
use Albert221\Blog\Widget\TwigWidgetExtension; |
11
|
|
|
use League\Container\ServiceProvider\AbstractServiceProvider; |
12
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
13
|
|
|
use Twig_Environment; |
14
|
|
|
use Twig_Loader_Filesystem; |
15
|
|
|
|
16
|
|
|
class TwigServiceProvider extends AbstractServiceProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
protected $provides = [ |
22
|
|
|
'twig', |
23
|
|
|
'twigWidgetExtension', |
24
|
|
|
'paginatorBuilder' |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function register() |
31
|
|
|
{ |
32
|
|
|
$this->getContainer()->share('twig', function () { |
33
|
|
|
$loader = new Twig_Loader_Filesystem( |
34
|
|
|
$this->getContainer()->get('baseDir').'/views' |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$config = [ |
38
|
|
|
'autoescape' => false |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
if (!$this->getContainer()->get('config')['debug']) { |
42
|
|
|
$config['cache'] = sprintf('%s/cache/twig', $this->getContainer()->get('baseDir')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$twig = new Twig_Environment($loader, $config); |
46
|
|
|
$twig->addGlobal('settings', $this->getContainer()->get(SettingRepositoryInterface::class)); |
47
|
|
|
|
48
|
|
|
$currentUrl = $this->getContainer()->get(ServerRequestInterface::class) |
49
|
|
|
->getUri()->withFragment('')->withQuery(''); |
50
|
|
|
$twig->addGlobal('currentUrl', $currentUrl); |
51
|
|
|
|
52
|
|
|
return $twig; |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
$this->getContainer()->share('twigWidgetExtension', function () { |
56
|
|
|
return new TwigWidgetExtension( |
57
|
|
|
$this->getContainer()->get('twig'), |
58
|
|
|
$this->getContainer()->get(PostRepositoryInterface::class), |
59
|
|
|
$this->getContainer()->get(CategoryRepositoryInterface::class), |
60
|
|
|
$this->getContainer()->get(TagRepositoryInterface::class) |
61
|
|
|
); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
$this->getContainer()->share('paginatorBuilder', function () { |
65
|
|
|
return new PaginatorBuilder( |
66
|
|
|
$this->getContainer()->get('twig'), |
67
|
|
|
$this->getContainer()->get(SettingRepositoryInterface::class)['pagination.per_page']->getValue() |
68
|
|
|
); |
69
|
|
|
}); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|