Completed
Push — develop ( 0b8425...c51867 )
by Jaap
15s queued 11s
created

src/phpDocumentor/Application.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor;
17
18
use Cilex\Application as Cilex;
19
use phpDocumentor\Event\Dispatcher;
20
use phpDocumentor\Parser\Event\PreFileEvent;
21
use Psr\Log\LoggerInterface;
22
use Psr\Log\LogLevel;
23
use RuntimeException;
24
use Symfony\Component\DependencyInjection\ContainerInterface;
25
26
/**
27
 * Application class for phpDocumentor.
28
 *
29
 * Can be used as bootstrap when the run method is not invoked.
30
 *
31
 * @codeCoverageIgnore too many side-effects and system calls to properly test
32
 */
33
class Application extends Cilex
34
{
35
    public static function VERSION(): string
36
    {
37
        return trim(file_get_contents(__DIR__ . '/../../VERSION'));
38
    }
39
40
    public static function templateDirectory(): string
41
    {
42
        $templateDir = __DIR__ . '/../../data/templates';
43
44
        // when installed using composer the templates are in a different folder
45
        $composerTemplatePath = __DIR__ . '/../../../templates';
46
        if (file_exists($composerTemplatePath)) {
47
            $templateDir = $composerTemplatePath;
48
        }
49
50
        return $templateDir;
51
    }
52
53
    /**
54
     * Initializes all components used by phpDocumentor.
55
     */
56
    public function __construct(LoggerInterface $logger, ContainerInterface $container)
57
    {
58
        parent::__construct($container);
59
60
        $this->defineIniSettings();
61
        $this->register(new Plugin\ServiceProvider());
62
63
        Dispatcher::getInstance()->addListener(
64
            'parser.file.pre',
65
            function (PreFileEvent $event) use ($logger) {
66
                $logger->log(LogLevel::NOTICE, 'Parsing ' . $event->getFile());
67
            }
68
        );
69
70
        $this->container = $container;
71
    }
72
73
    /**
74
     * Adjust php.ini settings.
75
     *
76
     * @throws RuntimeException
77
     */
78
    protected function defineIniSettings(): void
79
    {
80
        $this->setTimezone();
81
        ini_set('memory_limit', '-1');
82
83
        if (extension_loaded('Zend OPcache') && ini_get('opcache.enable') && ini_get('opcache.enable_cli')) {
84
            if (ini_get('opcache.save_comments')) {
85
                ini_set('opcache.load_comments', '1');
86
            } else {
87
                ini_set('opcache.enable', '0');
88
            }
89
        }
90
91
        if (extension_loaded('Zend Optimizer+') && ini_get('zend_optimizerplus.save_comments') === 0) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of ini_get('zend_optimizerplus.save_comments') (string) and 0 (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
92
            throw new RuntimeException('Please enable zend_optimizerplus.save_comments in php.ini.');
93
        }
94
    }
95
96
    /**
97
     * If the timezone is not set anywhere, set it to UTC.
98
     *
99
     * This is done to prevent any warnings being outputted in relation to using
100
     * date/time functions.
101
     *
102
     * @link http://php.net/manual/en/function.date-default-timezone-get.php for more information how PHP determines the
103
     *     default timezone.
104
     */
105
    protected function setTimezone(): void
106
    {
107
        if (false === ini_get('date.timezone')) {
108
            date_default_timezone_set('UTC');
109
        }
110
    }
111
}
112