From 11fc6054b5fbbb96ca72feaf45d03bbe610157db Mon Sep 17 00:00:00 2001
From: diosmosis <benaka@piwik.pro>
Date: Sun, 19 Apr 2015 21:51:18 -0700
Subject: [PATCH] Allow plugins to have environment DI config files. Move all
 logging DI to Monolog plugin and only use logging in tracker if [Tracker]
 debug = 1.

---
 config/environment/cli.php          | 29 -----------------------------
 core/Container/ContainerFactory.php | 16 +++++++++++-----
 piwik.php                           |  4 ++++
 plugins/Monolog/config/config.php   |  4 +++-
 4 files changed, 18 insertions(+), 35 deletions(-)
 delete mode 100644 config/environment/cli.php

diff --git a/config/environment/cli.php b/config/environment/cli.php
deleted file mode 100644
index 0db3009ad4..0000000000
--- a/config/environment/cli.php
+++ /dev/null
@@ -1,29 +0,0 @@
-<?php
-
-use Interop\Container\ContainerInterface;
-use Monolog\Logger;
-use Symfony\Bridge\Monolog\Formatter\ConsoleFormatter;
-use Symfony\Bridge\Monolog\Handler\ConsoleHandler;
-use Symfony\Component\Console\Output\OutputInterface;
-
-return array(
-
-    // Log
-    'log.handlers' => array(
-        DI\get('Symfony\Bridge\Monolog\Handler\ConsoleHandler'),
-    ),
-    'Symfony\Bridge\Monolog\Handler\ConsoleHandler' => function (ContainerInterface $c) {
-        // Override the default verbosity map to make it more verbose by default
-        $verbosityMap = array(
-            OutputInterface::VERBOSITY_NORMAL => Logger::INFO,
-            OutputInterface::VERBOSITY_VERBOSE => Logger::DEBUG,
-            OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::DEBUG,
-            OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG,
-        );
-        $handler = new ConsoleHandler(null, true, $verbosityMap);
-        $handler->setFormatter(new ConsoleFormatter($c->get('log.console.format'), null, true, true));
-        return $handler;
-    },
-    'log.console.format' => '%start_tag%%level_name% %extra.class%[%datetime%]%end_tag% %message%' . PHP_EOL,
-
-);
diff --git a/core/Container/ContainerFactory.php b/core/Container/ContainerFactory.php
index af2a09b695..9abad4f5ce 100644
--- a/core/Container/ContainerFactory.php
+++ b/core/Container/ContainerFactory.php
@@ -90,7 +90,9 @@ class ContainerFactory
 
         $file = sprintf('%s/config/environment/%s.php', PIWIK_USER_PATH, $this->environment);
 
-        $builder->addDefinitions($file);
+        if (file_exists($file)) {
+            $builder->addDefinitions($file);
+        }
     }
 
     private function addPluginConfigs(ContainerBuilder $builder)
@@ -98,13 +100,17 @@ class ContainerFactory
         $plugins = Manager::getInstance()->getActivatedPluginsFromConfig();
 
         foreach ($plugins as $plugin) {
-            $file = Manager::getPluginsDirectory() . $plugin . '/config/config.php';
+            $baseDir = Manager::getPluginsDirectory() . $plugin;
 
-            if (! file_exists($file)) {
-                continue;
+            $file = $baseDir . '/config/config.php';
+            if (file_exists($file)) {
+                $builder->addDefinitions($file);
             }
 
-            $builder->addDefinitions($file);
+            $environmentFile = $baseDir . '/config/' . $this->environment . '.php';
+            if (file_exists($environmentFile)) {
+                $builder->addDefinitions($environmentFile);
+            }
         }
     }
 }
diff --git a/piwik.php b/piwik.php
index 95cc6271f1..dcca10e408 100644
--- a/piwik.php
+++ b/piwik.php
@@ -48,6 +48,10 @@ require_once PIWIK_INCLUDE_PATH . '/core/Tracker/Cache.php';
 require_once PIWIK_INCLUDE_PATH . '/core/Tracker/Request.php';
 require_once PIWIK_INCLUDE_PATH . '/core/Cookie.php';
 
+\Piwik\Container\StaticContainer::setEnvironment('tracker');
+
+\Piwik\Profiler::setupProfilerXHProf(true, true);
+
 Tracker::loadTrackerEnvironment();
 
 $tracker    = new Tracker();
diff --git a/plugins/Monolog/config/config.php b/plugins/Monolog/config/config.php
index 3fd3fcf63a..22cf7debee 100644
--- a/plugins/Monolog/config/config.php
+++ b/plugins/Monolog/config/config.php
@@ -6,9 +6,11 @@ use Piwik\Log;
 
 return array(
 
-    'Psr\Log\LoggerInterface' => DI\object('Monolog\Logger')
+    'Monolog\Logger' => DI\object('Monolog\Logger')
         ->constructor('piwik', DI\get('log.handlers'), DI\get('log.processors')),
 
+    'Psr\Log\LoggerInterface' => DI\get('Monolog\Logger'),
+
     'log.handlers' => DI\factory(function (ContainerInterface $c) {
         if ($c->has('ini.log.log_writers')) {
             $writerNames = $c->get('ini.log.log_writers');
-- 
GitLab