Skip to content

Commit

Permalink
allow edition of the lockfile path in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Kharhamel committed Sep 24, 2019
1 parent e53a414 commit b810044
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 43 deletions.
5 changes: 5 additions & 0 deletions src/Commands/AlteredConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,9 @@ public function getAnnotationParser(): AnnotationParser
{
return $this->configuration->getAnnotationParser();
}

public function getLockFilePath(): ?string
{
return $this->configuration->getLockFilePath();
}
}
8 changes: 6 additions & 2 deletions src/Commands/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ protected function configure()
->setDescription('Generates DAOs and beans.')
->setHelp('Use this command to generate or regenerate the DAOs and beans for your project.')
->addOption(
'from-lock',null,InputOption::VALUE_OPTIONAL, 'Load the schema from the lock file instead of database',false
);
'from-lock',
null,
InputOption::VALUE_OPTIONAL,
'Load the schema from the lock file instead of database',
false
)
;
}

Expand Down
10 changes: 9 additions & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Configuration implements ConfigurationInterface
* @var AnnotationParser
*/
private $annotationParser;
private $lockFilePath;

/**
* @param string $beanNamespace The namespace hosting the beans
Expand All @@ -81,9 +82,10 @@ class Configuration implements ConfigurationInterface
* @param GeneratorListenerInterface[] $generatorListeners A list of listeners that will be triggered when beans/daos are generated
* @param AnnotationParser|null $annotationParser
* @param CodeGeneratorListenerInterface[] $codeGeneratorListeners A list of listeners that can alter code generation of each bean/dao
* @param string|null $lockFilePath
* @throws \Mouf\Database\SchemaAnalyzer\SchemaAnalyzerException
*/
public function __construct(string $beanNamespace, string $daoNamespace, Connection $connection, NamingStrategyInterface $namingStrategy = null, Cache $cache = null, SchemaAnalyzer $schemaAnalyzer = null, LoggerInterface $logger = null, array $generatorListeners = [], AnnotationParser $annotationParser = null, array $codeGeneratorListeners = [])
public function __construct(string $beanNamespace, string $daoNamespace, Connection $connection, NamingStrategyInterface $namingStrategy = null, Cache $cache = null, SchemaAnalyzer $schemaAnalyzer = null, LoggerInterface $logger = null, array $generatorListeners = [], AnnotationParser $annotationParser = null, array $codeGeneratorListeners = [], string $lockFilePath = null)
{
$this->beanNamespace = rtrim($beanNamespace, '\\');
$this->daoNamespace = rtrim($daoNamespace, '\\');
Expand All @@ -104,6 +106,7 @@ public function __construct(string $beanNamespace, string $daoNamespace, Connect
$this->annotationParser = $annotationParser ?: AnnotationParser::buildWithDefaultAnnotations([]);
$this->codeGeneratorListener = new CodeGeneratorEventDispatcher($codeGeneratorListeners);
$this->namingStrategy = $namingStrategy ?: new DefaultNamingStrategy($this->annotationParser, $this->connection->getSchemaManager());
$this->lockFilePath = $lockFilePath;
}

/**
Expand Down Expand Up @@ -214,4 +217,9 @@ public function getAnnotationParser(): AnnotationParser
{
return $this->annotationParser;
}

public function getLockFilePath(): ?string
{
return $this->lockFilePath;
}
}
2 changes: 2 additions & 0 deletions src/ConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ public function getPathFinder(): PathFinderInterface;
* @return AnnotationParser
*/
public function getAnnotationParser(): AnnotationParser;

public function getLockFilePath(): ?string;
}
22 changes: 14 additions & 8 deletions src/TDBMSchemaAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
*/
class TDBMSchemaAnalyzer
{
const schemaFileName = 'tdbm.lock.yml';

private $lockFilePath;
private $connection;

/**
Expand Down Expand Up @@ -56,12 +55,13 @@ class TDBMSchemaAnalyzer
* @param SchemaAnalyzer $schemaAnalyzer The schema analyzer that will be used to find shortest paths...
* Will be automatically created if not passed
*/
public function __construct(Connection $connection, Cache $cache, SchemaAnalyzer $schemaAnalyzer)
public function __construct(Connection $connection, Cache $cache, SchemaAnalyzer $schemaAnalyzer, ?string $lockFilePath = null)
{
$this->connection = $connection;
$this->cache = $cache;
$this->schemaAnalyzer = $schemaAnalyzer;
$this->schemaVersionControlService = new SchemaVersionControlService($this->connection, self::getLockFilePath());
$this->lockFilePath = $lockFilePath ?: self::getDefaultLockFilePath();
$this->schemaVersionControlService = new SchemaVersionControlService($this->connection, $this->lockFilePath);
}

/**
Expand All @@ -78,10 +78,15 @@ public function getCachePrefix(): string
return $this->cachePrefix;
}

//todo: in config
public static function getLockFilePath(): string

public static function getDefaultLockFilePath(): string
{
return RootProjectLocator::getRootLocationPath().'tdbm.lock.yml';
}

public function getLockFilePath(): string
{
return RootProjectLocator::getRootLocationPath().self::schemaFileName;
return $this->lockFilePath;
}

/**
Expand All @@ -93,7 +98,7 @@ public function getSchema(bool $ignoreCache = false): Schema
$cacheKey = $this->getCachePrefix().'_immutable_schema';
if (!$ignoreCache && $this->cache->contains($cacheKey)) {
$this->schema = $this->cache->fetch($cacheKey);
} elseif (!file_exists(self::getLockFilePath())) {
} elseif (!file_exists($this->getLockFilePath())) {
throw new TDBMException('No tdbm lock file found. Please regenerate DAOs and Beans.');
} else {
$this->schema = $this->schemaVersionControlService->loadSchemaFile();
Expand All @@ -108,6 +113,7 @@ public function getSchema(bool $ignoreCache = false): Schema
public function generateLockFile(): void
{
$this->schemaVersionControlService->dumpSchema();
\chmod($this->getLockFilePath(), 0664);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/TDBMService.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,11 @@ public function __construct(ConfigurationInterface $configuration)
$this->connection = $configuration->getConnection();
$this->cache = $configuration->getCache();
$this->schemaAnalyzer = $configuration->getSchemaAnalyzer();
$lockFilePath = $configuration->getLockFilePath();

$this->magicQuery = new MagicQuery($this->connection, $this->cache, $this->schemaAnalyzer);

$this->tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->connection, $this->cache, $this->schemaAnalyzer);
$this->tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->connection, $this->cache, $this->schemaAnalyzer, $lockFilePath);
$this->cachePrefix = $this->tdbmSchemaAnalyzer->getCachePrefix();

$this->toSaveObjects = new \SplObjectStorage();
Expand Down
35 changes: 6 additions & 29 deletions tests/Commands/GenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,22 @@

class GenerateCommandTest extends TDBMAbstractServiceTest
{
public static function getInputDefinition()
{
return new InputDefinition([
]);
}

public function testCall(): void
{
$input = new ArrayInput([
], self::getInputDefinition());
$input = new ArrayInput([], new InputDefinition([]));
$output = new BufferedOutput();
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);

//let's delete the lock file
$schemaFilePath = TDBMSchemaAnalyzer::getLockFilePath();
$schemaFilePath = TDBMSchemaAnalyzer::getDefaultLockFilePath();
if (file_exists($schemaFilePath)) {
unlink($schemaFilePath);
}
$result = $this->callCommand(new GenerateCommand($this->getConfiguration()), $input);
(new GenerateCommand($this->getConfiguration()))->run($input, $output);
$result = $output->fetch();

$this->assertContains('Finished regenerating DAOs and beans', $result);
//Check that the lock file was generated
$this->assertFileExists($schemaFilePath);
}

/**
* Calls the command passed in parameter. Returns the output.
*
* @param Command $command
* @param InputInterface $input
* @return string
*/
protected function callCommand(Command $command, InputInterface $input) : string
{
$output = new BufferedOutput();
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);

$r = new \ReflectionMethod($command, 'execute');
$r->setAccessible(true);
$r->invoke($command, $input, $output);

return $output->fetch();
}
}
4 changes: 2 additions & 2 deletions tests/TDBMDaoGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function setUp(): void
public function testGetSchemaCrashWithoutLock()
{
//let's delete the lock file
$schemaFilePath = TDBMSchemaAnalyzer::getLockFilePath();
$schemaFilePath = TDBMSchemaAnalyzer::getDefaultLockFilePath();
if (file_exists($schemaFilePath)) {
unlink($schemaFilePath);
}
Expand All @@ -129,7 +129,7 @@ public function testDaoGeneration(): void
$this->assertFileExists($dummyFile);

//let's delete the lock file
$schemaFilePath = TDBMSchemaAnalyzer::getLockFilePath();
$schemaFilePath = TDBMSchemaAnalyzer::getDefaultLockFilePath();
if (file_exists($schemaFilePath)) {
unlink($schemaFilePath);
}
Expand Down

0 comments on commit b810044

Please sign in to comment.