Skip to content
Browse files

Changing how to create a logger in order to use a class coming from t…

…he service locator.
  • Loading branch information...
1 parent 047c047 commit ae1faa6ea770f7e209124dfa0a11a9aba049f09d @jhuet jhuet committed Dec 1, 2012
View
5 config/module.doctrine-mongo-odm.local.php.dist
@@ -29,10 +29,9 @@ return array(
//
// 'default_db' => null,
//
-// 'filters' => array() // array('filterName' => 'BSON\Filter\Class'),
+// 'filters' => array(), // array('filterName' => 'BSON\Filter\Class'),
//
-// 'logger_callable' => function (array $log, \Zend\ServiceManager\ServiceLocatorInterface $sl) {
-// })
+// 'logger' => null // 'DoctrineMongoODMModule\Logging\DebugStack'
)
),
View
47 src/DoctrineMongoODMModule/Logging/DebugStack.php
@@ -0,0 +1,47 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+namespace DoctrineMongoODMModule\Logging;
+
+/**
+ * Includes executed queries in a Debug Stack
+ *
+ * @license MIT
+ * @link www.doctrine-project.org
+ */
+class DebugStack implements Logger
+{
+ /** @var array $queries Executed queries. */
+ public $queries = array();
+
+ /** @var boolean $enabled If Debug Stack is enabled (log queries) or not. */
+ public $enabled = true;
+
+ protected $currentQuery = 0;
+
+ /**
+ * {@inheritdoc}
+ */
+ public function log(array $logs)
+ {
+ if ($this->enabled) {
+ $this->queries[++$this->currentQuery] = $logs;
+ }
+ }
+}
+
View
36 src/DoctrineMongoODMModule/Logging/Logger.php
@@ -0,0 +1,36 @@
+<?php
+/*
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+namespace DoctrineMongoODMModule\Logging;
+
+/**
+ * Interface for loggers.
+ *
+ * @license MIT
+ * @link www.doctrine-project.org
+ */
+interface Logger
+{
+ /**
+ * Logs a SQL statement somewhere.
+ *
+ * @param array $logs The logs explaining the query
+ * @return void
+ */
+ public function log(array $logs);
+}
View
16 src/DoctrineMongoODMModule/Options/Configuration.php
@@ -104,9 +104,9 @@ class Configuration extends AbstractOptions
/**
*
- * @var mixed
+ * @var \DoctrineMongoODMModule\Logging\Logger
*/
- protected $loggerCallable;
+ protected $logger;
/**
*
@@ -295,21 +295,21 @@ public function setFilters(array $filters) {
/**
*
- * @param mixed $loggerCallable
+ * @param \DoctrineMongoODMModule\Logging\Logger $logger
* @return \DoctrineMongoODMModule\Options\Configuration
*/
- public function setLoggerCallable($loggerCallable)
+ public function setLogger($logger)
{
- $this->loggerCallable = $loggerCallable;
+ $this->logger = $logger;
return $this;
}
/**
*
- * @return mixed
+ * @return \DoctrineMongoODMModule\Logging\Logger
*/
- public function getLoggerCallable()
+ public function getLogger()
{
- return $this->loggerCallable;
+ return $this->logger;
}
}
View
9 src/DoctrineMongoODMModule/Service/ConfigurationFactory.php
@@ -46,11 +46,10 @@ public function createService(ServiceLocatorInterface $serviceLocator)
$config = new Configuration;
- // the logger with an injection of the service locator as a 2nd argument
- if (is_callable($options->getLoggerCallable())) {
- $config->setLoggerCallable(function(array $log) use ($options, $serviceLocator) {
- call_user_func_array($options->getLoggerCallable(), array($log, $serviceLocator));
- });
+ // logger
+ if ($options->getLogger()) {
+ $logger = $serviceLocator->get($options->getLogger());
+ $config->setLoggerCallable(array($logger, 'log'));
}
// proxies

0 comments on commit ae1faa6

Please sign in to comment.
Something went wrong with that request. Please try again.