Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Simplify configuration. Improve dependency injection.
Browse files Browse the repository at this point in the history
  • Loading branch information
aheinz-sg committed Sep 30, 2015
1 parent fd42d87 commit 4834611
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 27 deletions.
37 changes: 25 additions & 12 deletions DataCollector/XhprofCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

Expand All @@ -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'];
Expand All @@ -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
*
Expand Down
7 changes: 0 additions & 7 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 12 additions & 8 deletions Document/XhguiRuns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions Entity/XhguiRuns.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ public function save_run($xhprof_data, $type, $run_id = null) {

return $runId;
}
}
?>
60 changes: 60 additions & 0 deletions Tests/DataCollector/XhprofCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Jns\Bundle\XhprofBundle\Tests\DataCollector;

use Jns\Bundle\XhprofBundle\DataCollector\XhprofCollector;

class XhprofCollectorTest extends \PHPUnit_Framework_TestCase
{
/**
* @var XhprofCollector
*/
private $collector;

/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
private $container;

/**
* @var \Doctrine\Common\Persistence\ManagerRegistry
*/
private $doctrine;

public function setUp() {
parent::setUp();
$this->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);
}
}

0 comments on commit 4834611

Please sign in to comment.