Completed
Push — master ( e715c7...ffa9af )
by Craig
08:48 queued 02:34
created

DocController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 5
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
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 Zikula\Bundle\CoreInstallerBundle\Controller;
13
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\Routing\RouterInterface;
17
use Michelf\MarkdownExtra;
18
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
19
use Zikula\Common\Translator\TranslatorInterface;
20
use Zikula\Core\Response\PlainResponse;
21
22
/**
23
 * Class DocController
24
 */
25
class DocController
26
{
27
    /**
28
     * @var ZikulaHttpKernelInterface
29
     */
30
    private $kernel;
31
32
    /**
33
     * @var RouterInterface
34
     */
35
    private $router;
36
37
    /**
38
     * @var \Twig_Environment
39
     */
40
    private $twig;
41
42
    /**
43
     * @var MarkdownExtra
44
     */
45
    private $parser;
46
47
    /**
48
     * @var
49
     */
50
    private $basePath;
51
52
    /**
53
     * @var TranslatorInterface
54
     */
55
    private $translator;
56
57
    /**
58
     * Constructor.
59
     *
60
     * @param ZikulaHttpKernelInterface $kernel
61
     * @param RouterInterface $router The route generator
62
     * @param \Twig_Environment $twig
63
     * @param MarkdownExtra $parser
64
     * @param TranslatorInterface $translator
65
     */
66
    public function __construct(
67
        ZikulaHttpKernelInterface $kernel,
68
        RouterInterface $router,
69
        \Twig_Environment $twig,
70
        MarkdownExtra $parser,
71
        TranslatorInterface $translator
72
    ) {
73
        $this->kernel = $kernel;
74
        $this->router = $router;
75
        $this->twig = $twig;
76
        $this->parser = $parser;
77
        $this->translator = $translator;
78
    }
79
80
    /**
81
     * @param Request $request
82
     * @param string $name
83
     * @return Response
84
     */
85
    public function displayAction(Request $request, $name = 'INSTALL-1.4.md')
86
    {
87
        // @TODO this is temporary method of restricting the user input
88
        if (!in_array($name, ['INSTALL-1.4.md', 'UPGRADE-1.4.md', 'CHANGELOG.md', 'README.md'])) {
89
            $name = 'INSTALL-1.4.md';
90
        }
91
        $this->setBasePath($request);
92
93
        if (file_exists($this->basePath . "/$name")) {
94
            $content = file_get_contents($this->basePath . "/$name");
95
        } else {
96
            $content = $this->translator->__f('The file you requested (%s) could not be found.', ['%s' => "$name"]);
97
        }
98
        $content = $this->parser->defaultTransform($content);
99
        $templateParams = [
100
            'lang' => $request->getLocale(),
101
            'charset' => $this->kernel->getCharset(),
102
            'content' => $content,
103
        ];
104
        $response = new PlainResponse();
105
        $response->setContent($this->twig->render('ZikulaCoreInstallerBundle::doc.html.twig', $templateParams));
106
107
        return $response;
108
    }
109
110
    /**
111
     * set the base path for doc files, computing whether this is a Github clone or CI build.
112
     * @param Request $request
113
     */
114
    private function setBasePath(Request $request)
115
    {
116
        $paths = [
117
            $this->kernel->getRootDir() . '/../docs/' . $request->getLocale(), // localized in ci build
118
            $this->kernel->getRootDir() . '/../docs/en', // default in ci build
119
            $this->kernel->getRootDir() . '/../..' // github clone
120
        ];
121
        foreach ($paths as $docPath) {
122
            if ($path = realpath($docPath)) {
123
                $this->basePath = $path;
124
            }
125
        }
126
    }
127
}
128