FOSSology  4.4.0
Open Source License Compliance by Open Source Software
common-container.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
12 use Monolog\Handler\BrowserConsoleHandler;
13 use Monolog\Handler\ErrorLogHandler;
14 use Monolog\Logger;
16 use Symfony\Component\Config\FileLocator;
17 use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
18 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
19 
20 $restCall = (isset($GLOBALS['apiCall']) && $GLOBALS['apiCall']);
21 
22 $containerClassName = 'FossologyCachedContainer';
23 
24 $cacheDir = array_key_exists('CACHEDIR', $GLOBALS) ? $GLOBALS['CACHEDIR'] : null;
25 $cacheFile = "$cacheDir/container.php";
26 
27 $containerBuilder = "Symfony\Component\DependencyInjection\ContainerBuilder";
28 
29 $startTime = microtime(true);
30 
31 $cached = $cacheDir && file_exists($cacheFile);
32 
33 if ($cached) {
34  require_once ($cacheFile);
39  $container = new $containerClassName();
40 } else {
45  $container = new $containerBuilder();
46 
47  $container->setParameter('application_root', dirname(dirname(__DIR__)));
48 
49  $loader = new XmlFileLoader($container, new FileLocator(__DIR__));
50  $loader->load('services.xml');
51 
52  $container->compile();
53 
54  if ($cacheDir && is_dir($cacheDir)) {
55  $dumper = new PhpDumper($container);
56  umask(0027);
57  file_put_contents($cacheFile,
58  $dumper->dump(array(
59  'class' => $containerClassName
60  )));
61  }
62 }
63 
64 if ($restCall && $container->has('environment')) {
65  // Replace cached values with current values
66  $container->get('environment')->replace($_SERVER);
67 }
68 
69 $GLOBALS['container'] = $container;
70 $logger = $container->get('logger');
71 $logger->pushHandler(
72  new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, Logger::INFO));
73 $logger->pushHandler(new BrowserConsoleHandler(Logger::DEBUG));
74 
75 $timeZone = $container->getParameter('time.zone');
76 if (! empty($timeZone)) {
77  $twig = $container->get('twig.environment');
78  $twig->getExtension('core')->setTimezone($timeZone);
79 }
80 
82 $timingLogger = $container->get("log.timing");
83 $timingLogger->logWithStartTime(
84  sprintf("DI container setup (cached: %s)", $cached ? 'yes' : 'no'), $startTime);