Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
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 phpDocumentor\Event\Dispatcher;
19
use phpDocumentor\Parser\Event\PreFileEvent;
20
use Psr\Log\LoggerInterface;
21
use Psr\Log\LogLevel;
22
use RuntimeException;
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
25
/**
26
 * Application class for phpDocumentor.
27
 *
28
 * Can be used as bootstrap when the run method is not invoked.
29
 *
30
 * @codeCoverageIgnore too many side-effects and system calls to properly test
31
 */
32
class Application
33
{
34
    public static function VERSION(): string
35
    {
36
        return trim(file_get_contents(__DIR__ . '/../../VERSION'));
37
    }
38
39
    public static function templateDirectory(): string
40
    {
41
        $templateDir = __DIR__ . '/../../data/templates';
42
43
        // when installed using composer the templates are in a different folder
44
        $composerTemplatePath = __DIR__ . '/../../../templates';
45
        if (file_exists($composerTemplatePath)) {
46
            $templateDir = $composerTemplatePath;
47
        }
48
49
        return $templateDir;
50
    }
51
52
    /**
53
     * Initializes all components used by phpDocumentor.
54
     */
55
    public function __construct(LoggerInterface $logger, ContainerInterface $container)
56
    {
57
        $this->defineIniSettings();
58
59
        Dispatcher::getInstance()->addListener(
60
            'parser.file.pre',
61
            function (PreFileEvent $event) use ($logger) {
62
                $logger->log(LogLevel::NOTICE, 'Parsing ' . $event->getFile());
63
            }
64
        );
65
66
        $this->container = $container;
67
    }
68
69
    /**
70
     * Adjust php.ini settings.
71
     *
72
     * @throws RuntimeException
73
     */
74
    protected function defineIniSettings(): void
75
    {
76
        $this->setTimezone();
77
        ini_set('memory_limit', '-1');
78
79
        if (extension_loaded('Zend OPcache') && ini_get('opcache.enable') && ini_get('opcache.enable_cli')) {
80
            if (ini_get('opcache.save_comments')) {
81
                ini_set('opcache.load_comments', '1');
82
            } else {
83
                ini_set('opcache.enable', '0');
84
            }
85
        }
86
87
        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...
88
            throw new RuntimeException('Please enable zend_optimizerplus.save_comments in php.ini.');
89
        }
90
    }
91
92
    /**
93
     * If the timezone is not set anywhere, set it to UTC.
94
     *
95
     * This is done to prevent any warnings being outputted in relation to using
96
     * date/time functions.
97
     *
98
     * @link http://php.net/manual/en/function.date-default-timezone-get.php for more information how PHP determines the
99
     *     default timezone.
100
     */
101
    protected function setTimezone(): void
102
    {
103
        if (false === ini_get('date.timezone')) {
104
            date_default_timezone_set('UTC');
105
        }
106
    }
107
}
108