Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.3] Fixing generation of beans and DAOs from Mouf UI #144

Merged
merged 1 commit into from
May 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"phlib/logger": "^2.1",
"symfony/console": "^2.0 || ^3.0",
"mouf/utils.console": "^1.0",
"mouf/utils.log.psr.multi-logger": "^1.0"
"mouf/utils.log.psr.multi-logger": "^1.0",
"symfony/filesystem": "~2.7|~3.0"
},
"require-dev" : {
"phpunit/phpunit": "^5.5",
Expand Down
16 changes: 13 additions & 3 deletions src/Mouf/Database/TDBM/Utils/PathFinder/PathFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class PathFinder implements PathFinderInterface
*/
private $composerFile;

/**
* @var string
*/
private $rootPath;

/**
* @var bool
*/
Expand All @@ -20,16 +25,21 @@ class PathFinder implements PathFinderInterface
*/
private $classNameMapper;

public function __construct(string $composerFile = null, bool $useAutoloadDev = false)
public function __construct(string $composerFile = null, string $rootPath = null, bool $useAutoloadDev = false)
{
$this->composerFile = $composerFile;
$this->useAutoloadDev = $useAutoloadDev;
if ($rootPath === null) {
$this->rootPath = dirname(__DIR__, 9);
} else {
$this->rootPath = $rootPath;
}
}

private function getClassNameMapper() : ClassNameMapper
{
if ($this->classNameMapper === null) {
$this->classNameMapper = ClassNameMapper::createFromComposerFile($this->composerFile, null, $this->useAutoloadDev);
$this->classNameMapper = ClassNameMapper::createFromComposerFile($this->composerFile, $this->rootPath, $this->useAutoloadDev);
}
return $this->classNameMapper;
}
Expand All @@ -47,6 +57,6 @@ public function getPath(string $className): \SplFileInfo
if (empty($paths)) {
throw NoPathFoundException::create($className);
}
return new \SplFileInfo($paths[0]);
return new \SplFileInfo($this->rootPath.'/'.$paths[0]);
}
}
33 changes: 16 additions & 17 deletions src/Mouf/Database/TDBM/Utils/TDBMDaoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Mouf\Database\TDBM\TDBMException;
use Mouf\Database\TDBM\TDBMSchemaAnalyzer;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Filesystem\Filesystem;

/**
* This class generates automatically DAOs and Beans for TDBM.
Expand Down Expand Up @@ -106,7 +107,6 @@ public function generateAllDaosAndBeans(): void
*/
private function generateDaoAndBean(Table $table) : BeanDescriptor
{
// TODO: $storeInUtc is NOT USED.
$tableName = $table->getName();
$daoName = $this->namingStrategy->getDaoClassName($tableName);
$beanName = $this->namingStrategy->getBeanClassName($tableName);
Expand Down Expand Up @@ -136,9 +136,7 @@ public function generateBean(BeanDescriptor $beanDescriptor, $className, $baseCl

$possibleBaseFileName = $this->configuration->getPathFinder()->getPath($beannamespace.'\\Generated\\'.$baseClassName)->getPathname();

$this->ensureDirectoryExist($possibleBaseFileName);
file_put_contents($possibleBaseFileName, $str);
@chmod($possibleBaseFileName, 0664);
$this->dumpFile($possibleBaseFileName, $str);

$possibleFileName = $this->configuration->getPathFinder()->getPath($beannamespace.'\\'.$className)->getPathname();

Expand All @@ -161,9 +159,8 @@ class $className extends $baseClassName
{
}
";
$this->ensureDirectoryExist($possibleFileName);
file_put_contents($possibleFileName, $str);
@chmod($possibleFileName, 0664);

$this->dumpFile($possibleFileName, $str);
}
}

Expand Down Expand Up @@ -430,9 +427,7 @@ public function setDefaultSort(string \$defaultSort) : void

$possibleBaseFileName = $this->configuration->getPathFinder()->getPath($daonamespace.'\\Generated\\'.$baseClassName)->getPathname();

$this->ensureDirectoryExist($possibleBaseFileName);
file_put_contents($possibleBaseFileName, $str);
@chmod($possibleBaseFileName, 0664);
$this->dumpFile($possibleBaseFileName, $str);

$possibleFileName = $this->configuration->getPathFinder()->getPath($daonamespace.'\\'.$className)->getPathname();

Expand All @@ -456,9 +451,7 @@ class $className extends $baseClassName
{
}
";
$this->ensureDirectoryExist($possibleFileName);
file_put_contents($possibleFileName, $str);
@chmod($possibleFileName, 0664);
$this->dumpFile($possibleFileName, $str);
}
}

Expand Down Expand Up @@ -537,9 +530,7 @@ public function set'.$daoClassName.'('.$daoClassName.' $'.$daoInstanceName.') :

$possibleFileName = $this->configuration->getPathFinder()->getPath($daoNamespace.'\\Generated\\'.$daoFactoryClassName)->getPathname();

$this->ensureDirectoryExist($possibleFileName);
file_put_contents($possibleFileName, $str);
@chmod($possibleFileName, 0664);
$this->dumpFile($possibleFileName, $str);
}

/**
Expand Down Expand Up @@ -603,7 +594,7 @@ public static function toVariableName($str)
*
* @throws TDBMException
*/
private function ensureDirectoryExist($fileName)
private function ensureDirectoryExist(string $fileName)
{
$dirName = dirname($fileName);
if (!file_exists($dirName)) {
Expand All @@ -616,6 +607,14 @@ private function ensureDirectoryExist($fileName)
}
}

private function dumpFile(string $fileName, string $content) : void
{
$this->ensureDirectoryExist($fileName);
$fileSystem = new Filesystem();
$fileSystem->dumpFile($fileName, $content);
@chmod($fileName, 0664);
}

/**
* Transforms a DBAL type into a PHP type (for PHPDoc purpose).
*
Expand Down
2 changes: 2 additions & 0 deletions tests/Mouf/Database/TDBM/TDBMAbstractServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Mouf\Database\TDBM\Utils\DefaultNamingStrategy;
use Mouf\Database\TDBM\Utils\PathFinder\PathFinder;

abstract class TDBMAbstractServiceTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -98,6 +99,7 @@ protected function getConfiguration() : ConfigurationInterface

$this->dbConnection = DriverManager::getConnection($connectionParams, $config);
$this->configuration = new Configuration('Mouf\\Database\\TDBM\\Test\\Dao\\Bean', 'Mouf\\Database\\TDBM\\Test\\Dao', $this->dbConnection, $this->getNamingStrategy(), new ArrayCache(), null, null, [$this->getDummyGeneratorListener()]);
$this->configuration->setPathFinder(new PathFinder(null, dirname(__DIR__, 4)));
}
return $this->configuration;
}
Expand Down
9 changes: 6 additions & 3 deletions tests/Mouf/Database/TDBM/Utils/PathFinder/PathFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ class PathFinderTest extends \PHPUnit_Framework_TestCase
{
public function testGetPath()
{
$pathFinder = new PathFinder();
$this->assertSame('src/Mouf/Database/TDBM/Utils/PathFinder/PathFinder.php', $pathFinder->getPath(PathFinder::class)->getPathname());
$pathFinder = new PathFinder(null, dirname(__DIR__, 6));
$path = realpath($pathFinder->getPath(PathFinder::class)->getPathname());
$expectedPath = (new \ReflectionClass(PathFinder::class))->getFileName();

$this->assertSame($expectedPath, $path);
}

public function testGetPathNotFound()
{
$pathFinder = new PathFinder();
$pathFinder = new PathFinder(null, dirname(__DIR__, 6));
$this->expectException(NoPathFoundException::class);
$pathFinder->getPath("Not\\Exist");
}
Expand Down