FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
common-container.php
Go to the documentation of this file.
1 <?php
2 /*
3 Copyright (C) 2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
25 use Monolog\Logger;
32 
33 $restCall = (isset($GLOBALS['apiCall']) && $GLOBALS['apiCall']);
34 
35 $containerClassName = 'FossologyCachedContainer';
36 
37 $cacheDir = array_key_exists('CACHEDIR', $GLOBALS) ? $GLOBALS['CACHEDIR'] : null;
38 $cacheFile = "$cacheDir/container.php";
39 
40 $containerBuilder = "Symfony\Component\DependencyInjection\ContainerBuilder";
41 
42 $startTime = microtime(true);
43 
44 if ($restCall) {
45  // Call from REST api
46  $containerClassName = 'FossologyRestCachedContainer';
47  $cacheFile = "$cacheDir/rest-container.php";
48  $containerBuilder = "\Flexsounds\Component\SymfonyContainerSlimBridge\ContainerBuilder";
49 }
50 
51 $cached = $cacheDir && file_exists($cacheFile);
52 
53 if ($cached) {
54  require_once ($cacheFile);
55  $container = new $containerClassName();
56 } else {
57  $container = new $containerBuilder();
58 
59  $container->setParameter('application_root', dirname(dirname(__DIR__)));
60 
61  $loader = new XmlFileLoader($container, new FileLocator(__DIR__));
62  $loader->load('services.xml');
63 
64  if ($restCall) {
65  // Set error handlers for Slim
66  $container->set('notFoundHandler',
67  function ($request, $response){
68  $error = new Info(404, "Resource not found", InfoType::ERROR);
69  return $response->withJson($error->getArray(), $error->getCode());
70  });
71  $container->set('notAllowedHandler',
72  function ($request, $response, $methods){
73  $error = new Info(405,
74  'Method must be one of: ' . implode(', ', $methods), InfoType::ERROR);
75  return $response->withHeader('Allow', implode(', ', $methods))
76  ->withJson($error->getArray(), $error->getCode());
77  });
78  $container->set('phpErrorHandler',
79  function ($request, $response, $error){
80  $GLOBALS['container']->get('logger')
81  ->error($error);
82  $error = new Info(500, "Something went wrong! Please try again later.",
84  return $response->withJson($error->getArray(), $error->getCode());
85  });
86  $phpErrorHandler = $container->get('phpErrorHandler');
87  $container->set('errorHandler', $phpErrorHandler);
88  }
89 
90  $container->compile();
91 
92  if ($cacheDir && is_dir($cacheDir)) {
93  $dumper = new PhpDumper($container);
94  umask(0027);
95  file_put_contents($cacheFile,
96  $dumper->dump(array(
97  'class' => $containerClassName
98  )));
99  }
100 }
101 
102 if ($restCall) {
103  // Replace cached values with current values
104  $container->get('environment')->replace($_SERVER);
105 }
106 
107 $GLOBALS['container'] = $container;
108 $logger = $container->get('logger');
109 $logger->pushHandler(
110  new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, Logger::INFO));
111 $logger->pushHandler(new BrowserConsoleHandler(Logger::DEBUG));
112 
113 $timeZone = $container->getParameter('time.zone');
114 if (! empty($timeZone)) {
115  $twig = $container->get('twig.environment');
116  $twig->getExtension('core')->setTimezone($timeZone);
117 }
118 
120 $timingLogger = $container->get("log.timing");
121 $timingLogger->logWithStartTime(
122  sprintf("DI container setup (cached: %s)", $cached ? 'yes' : 'no'), $startTime);
#define ERROR(...)
Definition: logging.h:90
Info model to contain general error and return values.
Definition: Info.php:29