From 4834611304fb00f612ec4b5be2f19adcb554984d Mon Sep 17 00:00:00 2001 From: Adam Heinz Date: Wed, 30 Sep 2015 14:06:31 -0400 Subject: [PATCH] Simplify configuration. Improve dependency injection. --- DataCollector/XhprofCollector.php | 37 ++++++++----- DependencyInjection/Configuration.php | 7 --- Document/XhguiRuns.php | 20 ++++--- Entity/XhguiRuns.php | 1 + Tests/DataCollector/XhprofCollectorTest.php | 60 +++++++++++++++++++++ 5 files changed, 98 insertions(+), 27 deletions(-) create mode 100644 Tests/DataCollector/XhprofCollectorTest.php diff --git a/DataCollector/XhprofCollector.php b/DataCollector/XhprofCollector.php index 5d157c7..d1f4608 100644 --- a/DataCollector/XhprofCollector.php +++ b/DataCollector/XhprofCollector.php @@ -10,9 +10,10 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Bridge\Doctrine\ManagerRegistry; +use Doctrine\Common\Persistence\ManagerRegistry; use Jns\Bundle\XhprofBundle\Document\XhguiRuns as XhguiRuns_Document; use Jns\Bundle\XhprofBundle\Entity\XhguiRuns as XhguiRuns_Entity; +use Doctrine\ODM\MongoDB\DocumentManager; /** * XhprofDataCollector. @@ -113,24 +114,16 @@ public function stopProfiling($serverName, $uri) $this->collecting = false; - $enableXhgui = $this->container->getParameter('jns_xhprof.enable_xhgui'); - $xhprof_data = xhprof_disable(); if ($this->logger) { $this->logger->debug('Disabled XHProf'); } - $xhprof_runs = new \XHProfRuns_Default(); + $xhprof_runs = $this->createRun($serverName, $uri); $source = null; - if ($enableXhgui) { - $xhprof_runs = new XhguiRuns_Entity($serverName, $uri); - $xhprof_runs->setContainer($this->container); - } else if ($this->container->getParameter('jns_xhprof.enable_xhgui_new')) { - $xhprof_runs = new XhguiRuns_Document(); - $xhprof_runs->setContainer($this->container); - } else { + if ($xhprof_runs instanceof \XHProfRuns_Default) { $source = $this->sanitizeUriForSource($uri); } @@ -141,7 +134,7 @@ public function stopProfiling($serverName, $uri) 'source' => $source, ); - if ($this->container->getParameter('jns_xhprof.enable_xhgui_new')) { + if ($xhprof_runs instanceof XhguiRuns_Document) { $this->data['xhprof_url'] = $this->container->getParameter('jns_xhprof.location_web') . '/run/view?id=' . $this->data['xhprof']; } else { $this->data['xhprof_url'] = $this->container->getParameter('jns_xhprof.location_web') . '?run=' . $this->data['xhprof'] . '&source='.$this->data['source']; @@ -150,6 +143,26 @@ public function stopProfiling($serverName, $uri) return $this->data['xhprof']; } + /** + * @return \iXHProfRuns + */ + protected function createRun($serverName, $uri) { + $enableXhgui = $this->container->getParameter('jns_xhprof.enable_xhgui'); + if ($enableXhgui) { + $managerRegistry = $this->container->get($this->container->getParameter('jns_xhprof.manager_registry')); + $objectManager = $managerRegistry->getManager($this->container->getParameter('jns_xhprof.entity_manager')); + if ($objectManager instanceof DocumentManager) { + $xhprof_runs = new XhguiRuns_Document($objectManager); + } else { + $xhprof_runs = new XhguiRuns_Entity($serverName, $uri); + $xhprof_runs->setContainer($this->container); + } + } else { + $xhprof_runs = new \XHProfRuns_Default(); + } + return $xhprof_runs; + } + /** * Sanitize an uri to use it as source * diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 07a089a..2f80fd2 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -26,19 +26,12 @@ public function getConfigTree() $rootNode = $treeBuilder->root('jns_xhprof'); $rootNode - ->validate() - ->ifTrue(function($v) { - return $v['enable_xhgui'] && null === $v['entity_class']; - }) - ->thenInvalid('If you activate xhgui, you have to define an entity_class.') - ->end() ->children() ->scalarNode('location_web')->defaultValue('http://xhprof')->end() ->scalarNode('manager_registry')->defaultValue('doctrine')->end() ->scalarNode('entity_manager')->defaultValue('default')->end() ->scalarNode('entity_class')->defaultValue(null)->end() ->scalarNode('enable_xhgui')->defaultFalse()->end() - ->scalarNode('enable_xhgui_new')->defaultFalse()->end() ->arrayNode('exclude_patterns')->prototype('scalar')->end()->end() ->scalarNode('sample_size')->defaultValue(1)->end() ->scalarNode('enabled')->defaultFalse()->end() diff --git a/Document/XhguiRuns.php b/Document/XhguiRuns.php index 8ab4398..0d252e1 100644 --- a/Document/XhguiRuns.php +++ b/Document/XhguiRuns.php @@ -3,12 +3,18 @@ namespace Jns\Bundle\XhprofBundle\Document; use iXHProfRuns; -use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerAwareTrait; +use Doctrine\ODM\MongoDB\DocumentManager; -class XhguiRuns implements iXHProfRuns, ContainerAwareInterface +class XhguiRuns implements iXHProfRuns { - use ContainerAwareTrait; + /** + * @var DocumentManager + */ + private $documentManager; + + public function __construct(DocumentManager $documentManager) { + $this->documentManager = $documentManager; + } /** * {@inheritDoc} @@ -25,10 +31,8 @@ public function save_run($xhprof_data, $type, $run_id = null) { throw new \Exception('composer require perftools/xhgui dev-master'); } $data = $this->prepareForSave($xhprof_data); - $managerRegistry = $this->container->get($this->container->getParameter('jns_xhprof.manager_registry')); - $documentManager = $managerRegistry->getManager($this->container->getParameter('jns_xhprof.entity_manager')); - $dbname = $documentManager->getConfiguration()->getDefaultDB(); - $mongo = $documentManager->getConnection()->getMongoClient()->selectDB($dbname); + $dbname = $this->documentManager->getConfiguration()->getDefaultDB(); + $mongo = $this->documentManager->getConnection()->getMongoClient()->selectDB($dbname); $profiles = new \Xhgui_Profiles($mongo); $saver = new \Xhgui_Saver_Mongo($profiles); try { diff --git a/Entity/XhguiRuns.php b/Entity/XhguiRuns.php index 86b5c91..a99dafd 100644 --- a/Entity/XhguiRuns.php +++ b/Entity/XhguiRuns.php @@ -63,4 +63,5 @@ public function save_run($xhprof_data, $type, $run_id = null) { return $runId; } +} ?> diff --git a/Tests/DataCollector/XhprofCollectorTest.php b/Tests/DataCollector/XhprofCollectorTest.php new file mode 100644 index 0000000..54e2d93 --- /dev/null +++ b/Tests/DataCollector/XhprofCollectorTest.php @@ -0,0 +1,60 @@ +container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->disableOriginalConstructor()->getMock(); + $this->doctrine = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry'); + $this->collector = new XhprofCollectorTestable($this->container, null, $this->doctrine); + } + + /** + * @dataProvider data_createRun + */ + public function test_createRun($enable_xhgui, $manager_class, $runs_class) { + $this->container->method('get')->with('manager_registry')->will($this->returnValue($this->doctrine)); + $this->container->method('getParameter')->will($this->returnValueMap(array( + array('jns_xhprof.enable_xhgui', $enable_xhgui), + array('jns_xhprof.entity_manager', 'entity_manager'), + array('jns_xhprof.manager_registry', 'manager_registry'), + ))); + $this->doctrine->method('getManager')->with('entity_manager')->will($this->returnValue(!$manager_class ?: $this->getMock($manager_class))); + $xhprof_runs = $this->collector->createRun('serverName', 'uri'); + $this->assertInstanceOf($runs_class, $xhprof_runs); + } + + public function data_createRun() { + return array( + array(false, null, '\XHProfRuns_Default'), + array(true, '\Doctrine\ODM\MongoDB\DocumentManager', '\Jns\Bundle\XhprofBundle\Document\XhguiRuns'), + array(true, '\Doctrine\ORM\EntityManager', '\Jns\Bundle\XhprofBundle\Entity\XhguiRuns'), + ); + } +} + +class XhprofCollectorTestable extends XhprofCollector +{ + public function createRun($serverName, $uri) { + return parent::createRun($serverName, $uri); + } +} \ No newline at end of file