Skip to content

Commit

Permalink
EM: Define addInit as static function + automatic fix Sqlite cache pe…
Browse files Browse the repository at this point in the history
…rmission.
  • Loading branch information
janbarasek committed Dec 13, 2019
1 parent 012ad7f commit 30c2adb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
6 changes: 2 additions & 4 deletions src/DatabaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ public function afterCompile(ClassType $class): void
$initialize = $class->getMethod('initialize');

$initialize->setBody(
'/** @var ' . EntityManager::class . ' $entityManager */' . "\n"
. '$entityManager = $this->getByType(' . EntityManager::class . '::class);' . "\n"
. '$entityManager->addInit(function(Baraja\Doctrine\EntityManager $entityManager) {' . "\n"
EntityManager::class . '::addInit(function(' . EntityManager::class . ' $entityManager) {' . "\n"
. $this->getTypeDefinition() . "\n"
. "\t" . '$entityManager->setCache(' . $this->processCache() . ');' . "\n"
. "\t" . '$entityManager->getConnection()->getSchemaManager()->getDatabasePlatform()'
Expand Down Expand Up @@ -81,7 +79,7 @@ private function processCache(): string
if (extension_loaded('sqlite3')) {
return 'new ' . SQLite3Cache::class . '('
. '(function (Baraja\Doctrine\EntityManager $entityManager) {'
. "\n\t" . '$cache = new \SQLite3($entityManager->getDbDirPath() . \'/doctrine.db\');'
. "\n\t" . '$cache = new \SQLite3($entityManager->getDbDirPath());'
. "\n\t" . '$cache->busyTimeout(5000);'
. "\n\t" . '$cache->exec(\'PRAGMA journal_mode = wal;\');'
. "\n\t" . 'return $cache;'
Expand Down
14 changes: 12 additions & 2 deletions src/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\TransactionRequiredException;
use Doctrine\ORM\UnitOfWork;
use Nette\Utils\FileSystem;
use Tracy\Debugger;

/**
Expand Down Expand Up @@ -73,7 +74,7 @@ public function __construct(EntityManagerDependenciesAccessor $dependencies)
/**
* @param callable(self $entityManager) $callback
*/
final public function addInit(callable $callback): void
final public static function addInit(callable $callback): void
{
self::$onInit[] = $callback;
}
Expand All @@ -97,7 +98,16 @@ public function init(): void
*/
public function getDbDirPath(): string
{
return $this->dependencies->get()->getDbDirPath();
static $cache;

if ($cache === null) {
FileSystem::createDir($dir = \dirname(__DIR__, 4) . '/temp/cache/baraja.doctrine');
if (is_file($cache = $dir . '/doctrine.db') === true && fileperms($cache) < 33204) {
chmod($cache, 0664);
}
}

return $cache;
}

/**
Expand Down
15 changes: 0 additions & 15 deletions src/EntityManagerDependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Configuration;
use Nette\Utils\FileSystem;

class EntityManagerDependencies
{
Expand Down Expand Up @@ -63,18 +62,4 @@ public function getEventManager(): EventManager
return $this->eventManager;
}

/**
* @return string
*/
public function getDbDirPath(): string
{
static $cache;

if ($cache === null) {
FileSystem::createDir($cache = \dirname(__DIR__, 4) . '/temp/cache/baraja.doctrine');
}

return $cache;
}

}

0 comments on commit 30c2adb

Please sign in to comment.