Permalink
Browse files

Testing, documenting and completing integration with zendframework/ze…

…nd-developer-tools - waiting for zendframework/ZendDeveloperTools#24
  • Loading branch information...
1 parent 8a4c279 commit 161df3e4e3ba89907ce61b938c0230772949e0ad @Ocramius Ocramius committed Aug 3, 2012
View
@@ -49,17 +49,25 @@
'eventmanager' => array(
'orm_default' => array()
),
+
+ 'sql_logger_collector' => array(
+ 'orm_default' => array(),
+ ),
),
+ // zendframework/zend-developer-tools specific settings
+
'view_manager' => array(
'template_map' => array(
'zend-developer-tools/toolbar/doctrine-orm' => __DIR__ . '/../view/zend-developer-tools/toolbar/doctrine-orm.phtml',
),
),
'zdt' => array(
- 'collectors' => array(
- 'orm_default' => 'doctrine.collector.orm_default',
+ 'profiler' => array(
+ 'collectors' => array(
+ 'orm_default' => 'doctrine.sql_logger_collector.orm_default',
+ ),
),
'toolbar' => array(
'entries' => array(
View
@@ -0,0 +1,107 @@
+# Zend Developer Tools in DoctrineORMModule
+
+If you ever tried [Zend Developer Tools](https://github.com/zendframework/ZendDeveloperTools) you will surely understand
+the importance of being able to track performance pitfalls or excessive amount of queries in your applications when
+developing.
+
+## Setup
+
+To setup [Zend Developer Tools](https://github.com/zendframework/ZendDeveloperTools), run
+
+```sh
+php composer.phar require zendframework/zend-developer-tools dev-master
+```
+
+Then enable `ZendDeveloperTools` in your modules and enable profiling and the toolbar (see docs of Zend Developer Tools
+for that).
+
+Once `ZendDeveloperTools` is enabled, having `doctrine.entity_manager.orm_default` as your default `EntityManager`, you
+will notice that the queries performed by the ORM get logged and displayed in the toolbar.
+
+![](http://github.com/doctrine/DoctrineModule/raw/master/docs/images/zf2-zend-developer-tools-doctrine-module.png)
+
+## Customization
+
+If you want to customize this behavior (or track multiple `EntityManager` instances) you can do it in different ways.
+Please note that if you have set an `SQLLogger` in your configuration, this functionality won't override it, so you can
+use these features in total safety.
+
+### Multiple EntityManager/Connection instances and logging
+
+*WARNING! These are advanced features! Even if the code is fully tested, this is usually not required for most users!*
+
+To setup logging for an additional DBAL Connection or EntityManager, put something like following in your module:
+
+```php
+<?php
+
+namespace MyNamespace;
+
+class Module
+{
+ public function getConfig()
+ {
+ return array(
+ 'doctrine' => array(
+ 'sql_logger_collector' => array(
+ 'other_orm' => array(
+ // name of the sql logger collector (used by ZDT)
+ 'name' => 'other_orm',
+
+ // name of the configuration service at which to attach the logger
+ 'configuration' => 'doctrine.configuration.other_orm',
+
+ // uncomment following if you want to use a particular SQL logger instead of relying on
+ // the attached one
+ //'sql_logger' => 'service_name_of_my_dbal_sql_logger',
+ ),
+ ),
+ ),
+
+ 'zdt' => array(
+
+ // registering the profiler with ZDT
+ 'profiler' => array(
+ 'collectors' => array(
+ // reference to the service we have defined
+ 'other_orm' => 'doctrine.sql_logger_collector.other_orm',
+ ),
+ ),
+
+ // registering a new toolbar item with ZDT (name must be the same of the collector name)
+ 'toolbar' => array(
+ 'entries' => array(
+ // this is actually a name of a view script to use - you can use your custom one
+ 'other_orm' => 'zend-developer-tools/toolbar/doctrine-orm',
+ ),
+ ),
+ ),
+ );
+ }
+
+ public function getServiceConfiguration()
+ {
+ return array(
+ 'factories' => array(
+ // defining a service (any name is valid as long as you use it consistently across this example)
+ 'doctrine.sql_logger_collector.other_orm' => new \DoctrineORMModule\Service\SQLLoggerCollectorFactory('other_orm'),
+ ),
+ );
+ }
+
+ public function onBootstrap(\Zend\EventManager\EventInterface $e)
+ {
+ $config = $e->getTarget()->getServiceManager()->get('Config');
+
+ if (isset($config['zdt']['profiler']['enabled']) && $config['zdt']['profiler']['enabled']) {
+ // when ZDT is enabled, initialize the sql collector
+ $app->getServiceManager()->get('doctrine.sql_logger_collector.other_orm');
+ }
+ }
+}
+```
+
+This example will simply generate a new icon in the toolbar, with the log results of your `other_orm` connection:
+
+
+![](http://github.com/doctrine/DoctrineModule/raw/master/docs/images/zend-developer-tools-multiple-entity-managers.png)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -28,13 +28,9 @@
use DoctrineORMModule\Service\ConfigurationFactory as ORMConfigurationFactory;
use DoctrineORMModule\Service\EntityManagerFactory;
use DoctrineORMModule\Service\DBALConnectionFactory;
+use DoctrineORMModule\Service\SQLLoggerCollectorFactory;
use DoctrineORMModule\Form\Annotation\AnnotationBuilder;
-// @todo move to own factory to allow wrapping of controllers
-use DoctrineORMModule\Collector\SQLLoggerCollector;
-use Doctrine\DBAL\Logging\DebugStack;
-use Doctrine\DBAL\Logging\LoggerChain;
-
use Zend\ModuleManager\ModuleManagerInterface;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
@@ -104,6 +100,12 @@ public function onBootstrap(EventInterface $e)
$helperSet->set(new ConnectionHelper($em->getConnection()), 'db');
$helperSet->set(new EntityManagerHelper($em), 'em');
});
+
+ $config = $app->getServiceManager()->get('Config');
+
+ if (isset($config['zdt']['profiler']['enabled']) && $config['zdt']['profiler']['enabled']) {
+ $app->getServiceManager()->get('doctrine.sql_logger_collector.orm_default');
+ }
}
/**
@@ -124,32 +126,17 @@ public function getServiceConfig()
'Doctrine\ORM\EntityManager' => 'doctrine.entitymanager.orm_default',
),
'factories' => array(
+ 'doctrine.connection.orm_default' => new DBALConnectionFactory('orm_default'),
+ 'doctrine.configuration.orm_default' => new ORMConfigurationFactory('orm_default'),
+ 'doctrine.entitymanager.orm_default' => new EntityManagerFactory('orm_default'),
+
+ 'doctrine.driver.orm_default' => new DriverFactory('orm_default'),
+ 'doctrine.eventmanager.orm_default' => new EventManagerFactory('orm_default'),
+ 'doctrine.sql_logger_collector.orm_default' => new SQLLoggerCollectorFactory('orm_default'),
+
'DoctrineORMModule\Form\Annotation\AnnotationBuilder' => function(ServiceLocatorInterface $sl) {
return new AnnotationBuilder($sl->get('doctrine.entitymanager.orm_default'));
},
-
- 'doctrine.connection.orm_default' => new DBALConnectionFactory('orm_default'),
- 'doctrine.configuration.orm_default' => new ORMConfigurationFactory('orm_default'),
- 'doctrine.entitymanager.orm_default' => new EntityManagerFactory('orm_default'),
-
- 'doctrine.driver.orm_default' => new DriverFactory('orm_default'),
- 'doctrine.eventmanager.orm_default' => new EventManagerFactory('orm_default'),
- 'doctrine.collector.orm_default' => function(ServiceLocatorInterface $sl) {
- $debugStackLogger = new DebugStack();
- /* @var $configuration \Doctrine\ORM\Configuration */
- $configuration = $sl->get('doctrine.configuration.orm_default');
-
- if (null !== $configuration->getSQLLogger()) {
- $logger = new LoggerChain();
- $logger->addLogger($debugStackLogger);
- $logger->addLogger($configuration->getSQLLogger());
- $configuration->setSQLLogger($logger);
- } else {
- $configuration->setSQLLogger($debugStackLogger);
- }
-
- return new SQLLoggerCollector($debugStackLogger, 'orm_default');
- },
),
);
}
@@ -0,0 +1,101 @@
+<?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 DoctrineORMModule\Options;
+
+use Zend\Stdlib\AbstractOptions;
+
+/**
+ * Configuration options for an collector
+ *
+ * @license MIT
+ * @link http://www.doctrine-project.org/
+ * @author Marco Pivetta <ocramius@gmail.com>
+ */
+class SQLLoggerCollectorOptions extends AbstractOptions
+{
+ /**
+ * @var string name to be assigned to the collector
+ */
+ protected $name = 'orm_default';
+
+ /**
+ * @var string|null service name of the configuration where the logger has to be put
+ */
+ protected $configuration;
+
+ /**
+ * @var string|null service name of the SQLLogger to be used
+ */
+ protected $sqlLogger;
+
+ /**
+ * @param string $configuration
+ */
+ public function setName($name)
+ {
+ $this->name = (string) $name;
+ }
+
+ /**
+ * Name of the collector
+ *
+ * @return string
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
+ * @param string|null $configuration
+ */
+ public function setConfiguration($configuration)
+ {
+ $this->configuration = $configuration ? (string) $configuration : null;
+ }
+
+ /**
+ * Configuration service name (where to set the logger)
+ *
+ * @return string
+ */
+ public function getConfiguration()
+ {
+ return $this->configuration ? $this->configuration : 'doctrine.configuration.orm_default';
+ }
+
+ /**
+ * @param string|null $logger
+ */
+ public function setSqlLogger($sqlLogger)
+ {
+ $this->sqlLogger = $sqlLogger ? (string) $sqlLogger : null;
+ }
+
+ /**
+ * SQLLogger service name
+ *
+ * @return string|null
+ */
+ public function getSqlLogger()
+ {
+ return $this->sqlLogger;
+ }
+}
Oops, something went wrong.

0 comments on commit 161df3e

Please sign in to comment.