Skip to content

Commit

Permalink
[Doctrine]: use debug option instead of tracy check
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasPilar committed Feb 8, 2019
1 parent 9f8cf86 commit 431e4e2
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 50 deletions.
1 change: 1 addition & 0 deletions docs/en/index.md
Expand Up @@ -51,6 +51,7 @@ doctrine:
debug: %debugMode%
prefix: doctrine.default
proxyDir: %tempDir%/proxies
proxyNamespace: DoctrineProxies
sourceDir: %appDir%/Entity

entityManagerClassName: \Doctrine\ORM\EntityManager
Expand Down
37 changes: 16 additions & 21 deletions src/Adapter/Nette/DI/DoctrineExtension.php
Expand Up @@ -55,7 +55,6 @@
use Portiny\Doctrine\Contract\Provider\EntitySourceProviderInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
use Tracy\IBarPanel;

class DoctrineExtension extends CompilerExtension
{
Expand All @@ -76,17 +75,17 @@ class DoctrineExtension extends CompilerExtension
'dbal' => [
'type_overrides' => [],
'types' => [],
'schema_filter' => NULL,
'schema_filter' => null,
],
'prefix' => 'doctrine.default',
'proxyDir' => '%tempDir%/cache/proxies',
'proxyNamespace' => 'DoctrineProxies',
'sourceDir' => NULL,
'sourceDir' => null,
'entityManagerClassName' => EntityManager::class,
'defaultRepositoryClassName' => EntityRepository::class,
'repositoryFactory' => NULL,
'repositoryFactory' => null,
'namingStrategy' => UnderscoreNamingStrategy::class,
'sqlLogger' => NULL,
'sqlLogger' => null,
'targetEntityMappings' => [],
'metadata' => [],
'functions' => [],
Expand All @@ -96,7 +95,7 @@ class DoctrineExtension extends CompilerExtension
'resultCache' => 'default',
'hydrationCache' => 'default',
'secondLevelCache' => [
'enabled' => FALSE,
'enabled' => false,
'factoryClass' => DefaultCacheFactory::class,
'driver' => 'default',
'regions' => [
Expand Down Expand Up @@ -164,28 +163,28 @@ public function loadConfiguration(): void
$configurationDefinition->addSetup('setSQLLogger', ['@' . $name . '.sqlLogger']);
}

if ($config['metadataCache'] !== FALSE) {
if ($config['metadataCache'] !== false) {
$configurationDefinition->addSetup(
'setMetadataCacheImpl',
[$this->getCache($name . '.metadata', $builder, $config['metadataCache'])]
);
}

if ($config['queryCache'] !== FALSE) {
if ($config['queryCache'] !== false) {
$configurationDefinition->addSetup(
'setQueryCacheImpl',
[$this->getCache($name . '.query', $builder, $config['queryCache'])]
);
}

if ($config['resultCache'] !== FALSE) {
if ($config['resultCache'] !== false) {
$configurationDefinition->addSetup(
'setResultCacheImpl',
[$this->getCache($name . '.ormResult', $builder, $config['resultCache'])]
);
}

if ($config['hydrationCache'] !== FALSE) {
if ($config['hydrationCache'] !== false) {
$configurationDefinition->addSetup(
'setHydrationCacheImpl',
[$this->getCache($name . '.hydration', $builder, $config['hydrationCache'])]
Expand All @@ -208,7 +207,7 @@ public function loadConfiguration(): void
$builder->addDefinition($name . '.resolver')
->setType(ResolveTargetEntityListener::class);

if ($this->hasIBarPanelInterface()) {
if ($config['debug'] === true) {
$builder->addDefinition($this->prefix($name . '.diagnosticsPanel'))
->setType(self::DOCTRINE_SQL_PANEL);
}
Expand Down Expand Up @@ -243,11 +242,12 @@ public function beforeCompile(): void
*/
public function afterCompile(ClassType $classType): void
{
$config = $this->getConfig(self::$defaults);
$initialize = $classType->methods['initialize'];

$initialize->addBody('?::registerUniqueLoader("class_exists");', [new PhpLiteral(AnnotationRegistry::class)]);

if ($this->hasIBarPanelInterface()) {
if ($config['debug'] === true) {
$initialize->addBody('$this->getByType(\'' . self::DOCTRINE_SQL_PANEL . '\')->bindToBar();');
}

Expand Down Expand Up @@ -290,7 +290,7 @@ protected function processSecondLevelCache($name, array $config): void
$logger = $builder->addDefinition($this->prefix($name . '.cacheLogger'))
->setType(CacheLogger::class)
->setFactory(CacheLoggerChain::class)
->setAutowired(FALSE);
->setAutowired(false);

if ($config['logging']) {
$logger->addSetup('setLogger', ['statistics', new Statement(StatisticsCacheLogger::class)]);
Expand All @@ -301,7 +301,7 @@ protected function processSecondLevelCache($name, array $config): void
->setType(CacheConfiguration::class)
->addSetup('setCacheFactory', [$this->prefix('@' . $name . '.cacheFactory')])
->addSetup('setCacheLogger', [$this->prefix('@' . $name . '.cacheLogger')])
->setAutowired(FALSE);
->setAutowired(false);

$configuration = $builder->getDefinitionByType(Configuration::class);
$configuration->addSetup('setSecondLevelCacheEnabled');
Expand Down Expand Up @@ -369,7 +369,7 @@ private function getCache(string $prefix, ContainerBuilder $containerBuilder, st

$containerBuilder->addDefinition($prefix . '.redis')
->setType('\Redis')
->setAutowired(FALSE)
->setAutowired(false)
->addSetup('connect', [
$redisConfig['host'] ?? '127.0.0.1',
$redisConfig['port'] ?? null,
Expand All @@ -385,11 +385,6 @@ private function getCache(string $prefix, ContainerBuilder $containerBuilder, st
return '@' . $prefix . '.cache';
}

private function hasIBarPanelInterface(): bool
{
return interface_exists(IBarPanel::class);
}

private function registerCommandsIntoConsole(ContainerBuilder $containerBuilder, string $name): void
{
if ($this->hasSymfonyConsole()) {
Expand Down Expand Up @@ -479,6 +474,6 @@ private function hasSymfonyConsole(): bool
private function hasEventManager(ContainerBuilder $containerBuilder): bool
{
$eventManagerServiceName = $containerBuilder->getByType(EventManager::class);
return $eventManagerServiceName !== NULL && strlen($eventManagerServiceName) > 0;
return $eventManagerServiceName !== null && strlen($eventManagerServiceName) > 0;
}
}
6 changes: 3 additions & 3 deletions src/Adapter/Nette/Tracy/DoctrineSQLPanel.php
Expand Up @@ -60,7 +60,7 @@ final class DoctrineSQLPanel implements IBarPanel, SQLLogger
/**
* @var bool
*/
private $sortQueries = FALSE;
private $sortQueries = false;

/**
* @var array
Expand All @@ -80,7 +80,7 @@ public function __construct(EntityManager $entityManager)
/**
* {@inheritdoc}
*/
public function startQuery($sql, ?array $params = NULL, ?array $types = NULL): void
public function startQuery($sql, ?array $params = null, ?array $types = null): void
{
Debugger::timer('doctrine');

Expand All @@ -89,7 +89,7 @@ public function startQuery($sql, ?array $params = NULL, ?array $types = NULL): v
self::DATA_INDEX_PARAMS => $params,
self::DATA_INDEX_TYPES => $types,
self::DATA_INDEX_TIME => 0,
self::DATA_INDEX_TRACE => debug_backtrace(PHP_VERSION_ID >= 50306 ? DEBUG_BACKTRACE_IGNORE_ARGS : FALSE),
self::DATA_INDEX_TRACE => debug_backtrace(PHP_VERSION_ID >= 50306 ? DEBUG_BACKTRACE_IGNORE_ARGS : false),
];
}

Expand Down
4 changes: 2 additions & 2 deletions src/Adapter/Nette/Tracy/Helper.php
Expand Up @@ -10,7 +10,7 @@ final class Helper
/**
* Returns syntax highlighted SQL command.
*/
public static function dumpSql(string $sql, ?array $params = NULL, ?Connection $connection = NULL): string
public static function dumpSql(string $sql, ?array $params = null, ?Connection $connection = null): string
{
static $keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|'
. 'DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|SET|VALUES|LEFT\s+JOIN|'
Expand Down Expand Up @@ -71,7 +71,7 @@ public static function dumpSql(string $sql, ?array $params = NULL, ?Connection $
if ($type === 'stream') {
$info = stream_get_meta_data($param);
return '<i' . (isset($info['uri']) ? ' title="' .
htmlspecialchars($info['uri'], ENT_NOQUOTES, 'UTF-8') . '"' : NULL)
htmlspecialchars($info['uri'], ENT_NOQUOTES, 'UTF-8') . '"' : null)
. '>&lt;' . htmlspecialchars($type, ENT_NOQUOTES, 'UTF-8') . ' resource&gt;</i> ';
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Behaviour/Identifier.php
Expand Up @@ -16,7 +16,7 @@ trait Identifier

public function __clone()
{
$this->id = NULL;
$this->id = null;
}

/**
Expand Down
30 changes: 15 additions & 15 deletions src/Cache/DefaultCache.php
Expand Up @@ -20,14 +20,14 @@ final class DefaultCache extends CacheProvider
/**
* @var bool
*/
private $debugMode = FALSE;
private $debugMode = false;

/**
* @var Cache
*/
private $cache;

public function __construct(IStorage $storage, string $namespace = self::CACHE_NS, bool $debugMode = FALSE)
public function __construct(IStorage $storage, string $namespace = self::CACHE_NS, bool $debugMode = false)
{
$this->cache = new Cache($storage, $namespace);
$this->debugMode = $debugMode;
Expand All @@ -39,15 +39,15 @@ public function __construct(IStorage $storage, string $namespace = self::CACHE_N
protected function doFetch($id)
{
$cached = $this->cache->load($id);
return $cached !== NULL ? $cached : FALSE;
return $cached !== null ? $cached : false;
}

/**
* {@inheritdoc}
*/
protected function doContains($id)
{
return $this->cache->load($id) !== NULL;
return $this->cache->load($id) !== null;
}

/**
Expand All @@ -58,7 +58,7 @@ protected function doContains($id)
*/
protected function doSave($id, $data, $lifeTime = 0)
{
if ($this->debugMode !== TRUE) {
if ($this->debugMode !== true) {
return $this->doSaveDependingOnFiles($id, $data, [], $lifeTime);
}

Expand All @@ -72,7 +72,7 @@ protected function doSave($id, $data, $lifeTime = 0)

if (! empty($data)) {
$m = Strings::match($id, '#(?P<class>[^@$[\].]+)(?:\$(?P<prop>[^@$[\].]+))?\@\[Annot\]#i');
if ($m !== NULL && class_exists($m['class'])) {
if ($m !== null && class_exists($m['class'])) {
$files[] = self::getClassFilename($m['class']);
}
}
Expand All @@ -98,17 +98,17 @@ protected function doSaveDependingOnFiles($id, $data, array $files, $lifeTime =

$this->cache->save($id, $data, $dependencies);

return TRUE;
return true;
}

/**
* {@inheritdoc}
*/
protected function doDelete($id)
{
$this->cache->save($id, NULL);
$this->cache->save($id, null);

return TRUE;
return true;
}

/**
Expand All @@ -120,7 +120,7 @@ protected function doFlush()
Cache::TAGS => ['doctrine'],
]);

return TRUE;
return true;
}

/**
Expand All @@ -129,11 +129,11 @@ protected function doFlush()
protected function doGetStats()
{
return [
self::STATS_HITS => NULL,
self::STATS_MISSES => NULL,
self::STATS_UPTIME => NULL,
self::STATS_MEMORY_USAGE => NULL,
self::STATS_MEMORY_AVAILABLE => NULL,
self::STATS_HITS => null,
self::STATS_MISSES => null,
self::STATS_UPTIME => null,
self::STATS_MEMORY_USAGE => null,
self::STATS_MEMORY_AVAILABLE => null,
];
}

Expand Down
6 changes: 3 additions & 3 deletions src/Cache/RedisCache.php
Expand Up @@ -21,7 +21,7 @@ protected function doContains($id): bool
$value = $this->doFetch($id);
$this->prefetchCache[$id] = $value;

return $value !== FALSE;
return $value !== false;
}

/**
Expand All @@ -30,10 +30,10 @@ protected function doContains($id): bool
protected function doFetch($id)
{
// try load value from prefetch cache
$doFetch = $this->prefetchCache[$id] ?? NULL;
$doFetch = $this->prefetchCache[$id] ?? null;
$this->prefetchCache = [];

if ($doFetch === NULL) {
if ($doFetch === null) {
$doFetch = parent::doFetch($id);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Adapter/Nette/Tracy/DoctrineSQLPanelTest.php
Expand Up @@ -24,7 +24,7 @@ protected function setUp(): void

public function testStartQuery(): void
{
$this->doctrineSQLPanel->startQuery('SELECT 1 FROM dual', NULL, NULL);
$this->doctrineSQLPanel->startQuery('SELECT 1 FROM dual', null, null);
$this->doctrineSQLPanel->stopQuery();

$queries = $this->doctrineSQLPanel->getQueries();
Expand All @@ -34,7 +34,7 @@ public function testStartQuery(): void

public function testStopQuery(): void
{
$this->doctrineSQLPanel->startQuery('SELECT 1 FROM dual', NULL, NULL);
$this->doctrineSQLPanel->startQuery('SELECT 1 FROM dual', null, null);
sleep(1);
$this->doctrineSQLPanel->stopQuery();

Expand All @@ -51,7 +51,7 @@ public function testGetTab(): void
self::assertContains('<span title="Doctrine 2">', $this->doctrineSQLPanel->getTab());
self::assertContains('0 queries', $this->doctrineSQLPanel->getTab());

$this->doctrineSQLPanel->startQuery('SELECT 1 FROM dual', NULL, NULL);
$this->doctrineSQLPanel->startQuery('SELECT 1 FROM dual', null, null);
$this->doctrineSQLPanel->stopQuery();

self::assertContains('1 queries', $this->doctrineSQLPanel->getTab());
Expand All @@ -61,7 +61,7 @@ public function testGetPanel(): void
{
self::assertContains('<h2>Queries</h2>', $this->doctrineSQLPanel->getPanel());

$this->doctrineSQLPanel->startQuery('SELECT 1 FROM dual', NULL, NULL);
$this->doctrineSQLPanel->startQuery('SELECT 1 FROM dual', null, null);
$this->doctrineSQLPanel->stopQuery();

self::assertContains('<h1>Queries: 1, time:', $this->doctrineSQLPanel->getPanel());
Expand Down
2 changes: 1 addition & 1 deletion tests/ContainerFactory.php
Expand Up @@ -15,7 +15,7 @@ public static function create(): Container
$tempDir = __DIR__ . '/temp/' . getmypid();

if (! file_exists($tempDir . '/log')) {
mkdir($tempDir . '/log', 0777, TRUE);
mkdir($tempDir . '/log', 0777, true);
}

register_shutdown_function(function (): void {
Expand Down

0 comments on commit 431e4e2

Please sign in to comment.