-
-
Save willdurand/f1e02b7411d016abaf33 to your computer and use it in GitHub Desktop.
Kind of idea to load build properties in Propel2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This file is part of the Propel package. | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
* | |
* @license MIT License | |
*/ | |
namespace Propel\Generator\Command; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Propel\Generator\Config\GeneratorConfig; | |
use Propel\Generator\Config\Loader\XmlLoader; | |
/** | |
* @author William Durand <william.durand1@gmail.com> | |
*/ | |
abstract class AbstractCommand extends Command | |
{ | |
// … | |
/** | |
*/ | |
protected function loadGeneratorConfiguration() | |
{ | |
$conf = $this->getProjectDirectory() . DIRECTORY_SEPARATOR . self::GENERATOR_CONF_FILENAME; | |
if (!file_exists($conf)) { | |
throw new \RuntimeException(sprintf('File "%s" not found in "%s".', self::GENERATOR_CONF_FILENAME, $this->getProjectDirectory())); | |
} | |
$properties = array(); | |
// Strategy to choose a loader needed here | |
$loader = new XmlLoader(); | |
try { | |
$properties = $loader->parse(\simplexml_load_file($conf)); | |
} catch (\Exception $e) { | |
throw $e; | |
} | |
$this->generatorConfig->setBuildProperties($properties); | |
var_dump($this->generatorConfig->getBuildProperties()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<generator> | |
<project> | |
<name>bookstore</name> | |
<dir>.</dir> | |
</project> | |
<namespace> | |
<autoPackage>false</autoPackage> | |
</namespace> | |
<schema> | |
<autoPackage>false</autoPackage> | |
<autoNamespace>false</autoNamespace> | |
<autoPrefix>false</autoPrefix> | |
<validate>true</validate> | |
<transform>false</transform> | |
<tablePrefix></tablePrefix> | |
</schema> | |
<database> | |
<platform>mysql</platform> | |
<dsn>mysql:dbname=test</dsn> | |
<user></user> | |
<password></password> | |
<schema></schema> | |
<encoding></encoding> | |
<!-- | |
buildUrl ? | |
createUrl ? | |
--> | |
</database> | |
<!-- … ---> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Propel\Generator\Config\Loader; | |
/** | |
* @author William Durand <william.durand1@gmail.com> | |
*/ | |
interface LoaderInterface | |
{ | |
/** | |
* @return array | |
*/ | |
function parse($content); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ php bin/propel build:model --dir=tests/Fixtures/bookstore | |
array(11) { | |
["name"]=> | |
string(9) "bookstore" | |
["dir"]=> | |
string(1) "." | |
["namespaceAutoPackage"]=> | |
bool(false) | |
["database"]=> | |
string(5) "mysql" | |
["databasePlatform"]=> | |
string(5) "mysql" | |
["databaseDsn"]=> | |
string(17) "mysql:dbname=test" | |
["databaseUser"]=> | |
string(0) "" | |
["databasePassword"]=> | |
string(0) "" | |
["databaseSchema"]=> | |
string(0) "" | |
["databaseEncoding"]=> | |
string(0) "" | |
["databaseComment"]=> | |
string(0) "" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Propel\Generator\Config\Loader; | |
/** | |
* @author William Durand <william.durand1@gmail.com> | |
*/ | |
class XmlLoader implements LoaderInterface | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function parse($content) | |
{ | |
$properties = array(); | |
$content = (array) $content; | |
$project = (array) $content['project']; | |
$namespace = (array) $content['namespace']; | |
$database = (array) $content['database']; | |
foreach ($project as $k => $v) { | |
$properties['propel.'.$k] = $this->fixXmlValue((string) $v); | |
} | |
foreach ($namespace as $k => $v) { | |
$properties['propel.namespace.'.$k] = $this->fixXmlValue((string) $v); | |
} | |
foreach ($database as $k => $v) { | |
// | |
// Here is an example of a convenient change. | |
// | |
if ('platform' === strtolower($k)) { | |
$properties['propel.database'] = $this->fixXmlValue((string) $v); | |
} | |
$properties['propel.database.'.$k] = $this->fixXmlValue((string) $v); | |
} | |
// to be continued | |
return $properties; | |
} | |
/** | |
* Fixes XML value. | |
* | |
* @return string|boolean | |
*/ | |
protected function fixXmlValue($value) | |
{ | |
$lwr = strtolower($value); | |
if ('false' === $lwr) { | |
$value = false; | |
} else if ('true' === $lwr) { | |
$value = true; | |
} | |
return $value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment