Skip to content

Commit 51a9971

Browse files
committed
allow edition of the lockfile path in configuration
1 parent e53a414 commit 51a9971

11 files changed

+64
-60
lines changed

src/Commands/AlteredConfiguration.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,9 @@ public function getAnnotationParser(): AnnotationParser
138138
{
139139
return $this->configuration->getAnnotationParser();
140140
}
141+
142+
public function getLockFilePath(): string
143+
{
144+
return $this->configuration->getLockFilePath();
145+
}
141146
}

src/Commands/GenerateCommand.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ protected function configure()
3232
->setDescription('Generates DAOs and beans.')
3333
->setHelp('Use this command to generate or regenerate the DAOs and beans for your project.')
3434
->addOption(
35-
'from-lock',null,InputOption::VALUE_OPTIONAL, 'Load the schema from the lock file instead of database',false
36-
);
35+
'from-lock',
36+
null,
37+
InputOption::VALUE_OPTIONAL,
38+
'Load the schema from the lock file instead of database',
39+
false
40+
)
3741
;
3842
}
3943

src/Configuration.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinderInterface;
2222
use Psr\Log\LoggerInterface;
2323
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder;
24+
use TheCodingMachine\TDBM\Utils\RootProjectLocator;
2425

2526
class Configuration implements ConfigurationInterface
2627
{
@@ -69,6 +70,7 @@ class Configuration implements ConfigurationInterface
6970
* @var AnnotationParser
7071
*/
7172
private $annotationParser;
73+
private $lockFilePath;
7274

7375
/**
7476
* @param string $beanNamespace The namespace hosting the beans
@@ -81,9 +83,10 @@ class Configuration implements ConfigurationInterface
8183
* @param GeneratorListenerInterface[] $generatorListeners A list of listeners that will be triggered when beans/daos are generated
8284
* @param AnnotationParser|null $annotationParser
8385
* @param CodeGeneratorListenerInterface[] $codeGeneratorListeners A list of listeners that can alter code generation of each bean/dao
86+
* @param string|null $lockFilePath
8487
* @throws \Mouf\Database\SchemaAnalyzer\SchemaAnalyzerException
8588
*/
86-
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 = [])
89+
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)
8790
{
8891
$this->beanNamespace = rtrim($beanNamespace, '\\');
8992
$this->daoNamespace = rtrim($daoNamespace, '\\');
@@ -104,6 +107,7 @@ public function __construct(string $beanNamespace, string $daoNamespace, Connect
104107
$this->annotationParser = $annotationParser ?: AnnotationParser::buildWithDefaultAnnotations([]);
105108
$this->codeGeneratorListener = new CodeGeneratorEventDispatcher($codeGeneratorListeners);
106109
$this->namingStrategy = $namingStrategy ?: new DefaultNamingStrategy($this->annotationParser, $this->connection->getSchemaManager());
110+
$this->lockFilePath = $lockFilePath;
107111
}
108112

109113
/**
@@ -214,4 +218,14 @@ public function getAnnotationParser(): AnnotationParser
214218
{
215219
return $this->annotationParser;
216220
}
221+
222+
public static function getDefaultLockFilePath(): string
223+
{
224+
return RootProjectLocator::getRootLocationPath().'tdbm.lock.yml';
225+
}
226+
227+
public function getLockFilePath(): string
228+
{
229+
return $this->lockFilePath ?: self::getDefaultLockFilePath();
230+
}
217231
}

src/ConfigurationInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,6 @@ public function getPathFinder(): PathFinderInterface;
7373
* @return AnnotationParser
7474
*/
7575
public function getAnnotationParser(): AnnotationParser;
76+
77+
public function getLockFilePath(): string;
7678
}

src/TDBMSchemaAnalyzer.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414
use Doctrine\DBAL\Types\Type;
1515
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
1616
use TheCodingMachine\TDBM\Utils\ImmutableCaster;
17-
use TheCodingMachine\TDBM\Utils\RootProjectLocator;
1817

1918
/**
2019
* This class is used to analyze the schema and return valuable information / hints.
2120
*/
2221
class TDBMSchemaAnalyzer
2322
{
24-
const schemaFileName = 'tdbm.lock.yml';
25-
23+
private $lockFilePath;
2624
private $connection;
2725

2826
/**
@@ -51,17 +49,19 @@ class TDBMSchemaAnalyzer
5149
private $schemaVersionControlService;
5250

5351
/**
54-
* @param Connection $connection The DBAL DB connection to use
55-
* @param Cache $cache A cache service to be used
52+
* @param Connection $connection The DBAL DB connection to use
53+
* @param Cache $cache A cache service to be used
5654
* @param SchemaAnalyzer $schemaAnalyzer The schema analyzer that will be used to find shortest paths...
5755
* Will be automatically created if not passed
56+
* @param string $lockFilePath The path for the lock file which will store the database schema
5857
*/
59-
public function __construct(Connection $connection, Cache $cache, SchemaAnalyzer $schemaAnalyzer)
58+
public function __construct(Connection $connection, Cache $cache, SchemaAnalyzer $schemaAnalyzer, string $lockFilePath)
6059
{
6160
$this->connection = $connection;
6261
$this->cache = $cache;
6362
$this->schemaAnalyzer = $schemaAnalyzer;
64-
$this->schemaVersionControlService = new SchemaVersionControlService($this->connection, self::getLockFilePath());
63+
$this->lockFilePath = $lockFilePath;
64+
$this->schemaVersionControlService = new SchemaVersionControlService($this->connection, $this->lockFilePath);
6565
}
6666

6767
/**
@@ -78,10 +78,9 @@ public function getCachePrefix(): string
7878
return $this->cachePrefix;
7979
}
8080

81-
//todo: in config
82-
public static function getLockFilePath(): string
81+
public function getLockFilePath(): string
8382
{
84-
return RootProjectLocator::getRootLocationPath().self::schemaFileName;
83+
return $this->lockFilePath;
8584
}
8685

8786
/**
@@ -93,7 +92,7 @@ public function getSchema(bool $ignoreCache = false): Schema
9392
$cacheKey = $this->getCachePrefix().'_immutable_schema';
9493
if (!$ignoreCache && $this->cache->contains($cacheKey)) {
9594
$this->schema = $this->cache->fetch($cacheKey);
96-
} elseif (!file_exists(self::getLockFilePath())) {
95+
} elseif (!file_exists($this->getLockFilePath())) {
9796
throw new TDBMException('No tdbm lock file found. Please regenerate DAOs and Beans.');
9897
} else {
9998
$this->schema = $this->schemaVersionControlService->loadSchemaFile();
@@ -108,6 +107,7 @@ public function getSchema(bool $ignoreCache = false): Schema
108107
public function generateLockFile(): void
109108
{
110109
$this->schemaVersionControlService->dumpSchema();
110+
\chmod($this->getLockFilePath(), 0664);
111111
}
112112

113113
/**

src/TDBMService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,11 @@ public function __construct(ConfigurationInterface $configuration)
187187
$this->connection = $configuration->getConnection();
188188
$this->cache = $configuration->getCache();
189189
$this->schemaAnalyzer = $configuration->getSchemaAnalyzer();
190+
$lockFilePath = $configuration->getLockFilePath();
190191

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

193-
$this->tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->connection, $this->cache, $this->schemaAnalyzer);
194+
$this->tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->connection, $this->cache, $this->schemaAnalyzer, $lockFilePath);
194195
$this->cachePrefix = $this->tdbmSchemaAnalyzer->getCachePrefix();
195196

196197
$this->toSaveObjects = new \SplObjectStorage();

tests/Commands/GenerateCommandTest.php

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,33 @@
33

44
namespace TheCodingMachine\TDBM\Commands;
55

6+
use TheCodingMachine\TDBM\Configuration;
67
use TheCodingMachine\TDBM\TDBMAbstractServiceTest;
78
use Symfony\Component\Console\Command\Command;
89
use Symfony\Component\Console\Input\ArrayInput;
910
use Symfony\Component\Console\Input\InputDefinition;
1011
use Symfony\Component\Console\Input\InputInterface;
1112
use Symfony\Component\Console\Output\BufferedOutput;
1213
use Symfony\Component\Console\Output\OutputInterface;
13-
use TheCodingMachine\TDBM\TDBMSchemaAnalyzer;
1414

1515
class GenerateCommandTest extends TDBMAbstractServiceTest
1616
{
17-
public static function getInputDefinition()
18-
{
19-
return new InputDefinition([
20-
]);
21-
}
22-
2317
public function testCall(): void
2418
{
25-
$input = new ArrayInput([
26-
], self::getInputDefinition());
19+
$input = new ArrayInput([], new InputDefinition([]));
20+
$output = new BufferedOutput();
21+
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
2722

2823
//let's delete the lock file
29-
$schemaFilePath = TDBMSchemaAnalyzer::getLockFilePath();
24+
$schemaFilePath = Configuration::getDefaultLockFilePath();
3025
if (file_exists($schemaFilePath)) {
3126
unlink($schemaFilePath);
3227
}
33-
$result = $this->callCommand(new GenerateCommand($this->getConfiguration()), $input);
28+
(new GenerateCommand($this->getConfiguration()))->run($input, $output);
29+
$result = $output->fetch();
3430

3531
$this->assertContains('Finished regenerating DAOs and beans', $result);
3632
//Check that the lock file was generated
3733
$this->assertFileExists($schemaFilePath);
3834
}
39-
40-
/**
41-
* Calls the command passed in parameter. Returns the output.
42-
*
43-
* @param Command $command
44-
* @param InputInterface $input
45-
* @return string
46-
*/
47-
protected function callCommand(Command $command, InputInterface $input) : string
48-
{
49-
$output = new BufferedOutput();
50-
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
51-
52-
$r = new \ReflectionMethod($command, 'execute');
53-
$r->setAccessible(true);
54-
$r->invoke($command, $input, $output);
55-
56-
return $output->fetch();
57-
}
5835
}

tests/Performance/ManyToOneBench.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private static function generateDaosAndBeans(Connection $connection): void
9191
{
9292
$schemaManager = $connection->getSchemaManager();
9393
$schemaAnalyzer = new SchemaAnalyzer($schemaManager);
94-
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($connection, new ArrayCache(), $schemaAnalyzer);
94+
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($connection, new ArrayCache(), $schemaAnalyzer, Configuration::getDefaultLockFilePath());
9595
$tdbmDaoGenerator = new TDBMDaoGenerator(self::createConfiguration(), $tdbmSchemaAnalyzer);
9696
$rootPath = __DIR__ . '/../';
9797
self::recursiveDelete(__DIR__. '/../../src/Test/Dao/');

tests/TDBMDaoGeneratorTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ protected function setUp(): void
9898
parent::setUp();
9999
$schemaManager = $this->tdbmService->getConnection()->getSchemaManager();
100100
$schemaAnalyzer = new SchemaAnalyzer($schemaManager);
101-
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer);
101+
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer, Configuration::getDefaultLockFilePath());
102102
$this->tdbmDaoGenerator = new TDBMDaoGenerator($this->getConfiguration(), $tdbmSchemaAnalyzer);
103103
$this->rootPath = __DIR__ . '/../';
104104
//$this->tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json');
@@ -107,13 +107,13 @@ protected function setUp(): void
107107
public function testGetSchemaCrashWithoutLock()
108108
{
109109
//let's delete the lock file
110-
$schemaFilePath = TDBMSchemaAnalyzer::getLockFilePath();
110+
$schemaFilePath = Configuration::getDefaultLockFilePath();
111111
if (file_exists($schemaFilePath)) {
112112
unlink($schemaFilePath);
113113
}
114114
//let's check we cannot call get schema without a lock file
115115
$schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
116-
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), new ArrayCache(), $schemaAnalyzer);
116+
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), new ArrayCache(), $schemaAnalyzer, Configuration::getDefaultLockFilePath());
117117
$this->expectException('TheCodingMachine\TDBM\TDBMException');
118118
$schema1 = $tdbmSchemaAnalyzer->getSchema(true);
119119
}
@@ -129,7 +129,7 @@ public function testDaoGeneration(): void
129129
$this->assertFileExists($dummyFile);
130130

131131
//let's delete the lock file
132-
$schemaFilePath = TDBMSchemaAnalyzer::getLockFilePath();
132+
$schemaFilePath = Configuration::getDefaultLockFilePath();
133133
if (file_exists($schemaFilePath)) {
134134
unlink($schemaFilePath);
135135
}
@@ -169,7 +169,7 @@ public function testGenerationException(): void
169169

170170
$schemaManager = $this->tdbmService->getConnection()->getSchemaManager();
171171
$schemaAnalyzer = new SchemaAnalyzer($schemaManager);
172-
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer);
172+
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new ArrayCache(), $schemaAnalyzer, Configuration::getDefaultLockFilePath());
173173
$tdbmDaoGenerator = new TDBMDaoGenerator($configuration, $tdbmSchemaAnalyzer);
174174
$this->rootPath = __DIR__ . '/../../../../';
175175
//$tdbmDaoGenerator->setComposerFile($this->rootPath.'composer.json');

tests/TDBMSchemaAnalyzerTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function setUp(): void
3636
{
3737
parent::setUp();
3838
$schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
39-
$this->tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), new ArrayCache(), $schemaAnalyzer);
39+
$this->tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), new ArrayCache(), $schemaAnalyzer, Configuration::getDefaultLockFilePath());
4040
}
4141

4242
public function testSchemaLock(): void
@@ -51,7 +51,7 @@ public function testSchemaLock(): void
5151

5252
$schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
5353
$cache = new ArrayCache();
54-
$tdbmSchemaAnalyzer1 = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
54+
$tdbmSchemaAnalyzer1 = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer, Configuration::getDefaultLockFilePath());
5555

5656
$schemaFromAnalyser = $tdbmSchemaAnalyzer1->getSchema(true);
5757
$schemaFromAnalyserCached = $tdbmSchemaAnalyzer1->getSchema();
@@ -63,8 +63,8 @@ public function testGetSchema(): void
6363
{
6464
$schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
6565
$cache = new ArrayCache();
66-
$tdbmSchemaAnalyzer1 = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
67-
$tdbmSchemaAnalyzer2 = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
66+
$tdbmSchemaAnalyzer1 = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer, Configuration::getDefaultLockFilePath());
67+
$tdbmSchemaAnalyzer2 = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer, Configuration::getDefaultLockFilePath());
6868

6969
// Why don't we go in all lines of code????
7070
$schema1 = $tdbmSchemaAnalyzer1->getSchema();
@@ -77,7 +77,7 @@ public function testGetIncomingForeignKeys(): void
7777
{
7878
$schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
7979
$cache = new ArrayCache();
80-
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
80+
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer, Configuration::getDefaultLockFilePath());
8181

8282
$fks = $tdbmSchemaAnalyzer->getIncomingForeignKeys('users');
8383
$this->assertCount(1, $fks);
@@ -87,7 +87,7 @@ public function testGetIncomingForeignKeys2(): void
8787
{
8888
$schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
8989
$cache = new ArrayCache();
90-
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
90+
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer, Configuration::getDefaultLockFilePath());
9191

9292
$fks = $tdbmSchemaAnalyzer->getIncomingForeignKeys('contact');
9393
$this->assertCount(1, $fks);
@@ -97,7 +97,7 @@ public function testGetIncomingForeignKeys3(): void
9797
{
9898
$schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
9999
$cache = new ArrayCache();
100-
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
100+
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer, Configuration::getDefaultLockFilePath());
101101

102102
$fks = $tdbmSchemaAnalyzer->getIncomingForeignKeys('country');
103103
$this->assertCount(5, $fks);
@@ -112,7 +112,7 @@ public function testGetPivotTableLinkedToTable(): void
112112
{
113113
$schemaAnalyzer = new SchemaAnalyzer(self::getConnection()->getSchemaManager(), new ArrayCache(), 'prefix_');
114114
$cache = new ArrayCache();
115-
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
115+
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer, Configuration::getDefaultLockFilePath());
116116

117117
$pivotTables = $tdbmSchemaAnalyzer->getPivotTableLinkedToTable('rights');
118118
$this->assertCount(1, $pivotTables);

0 commit comments

Comments
 (0)