Permalink
Browse files

[Generator] [Schema] started POC for SchemaConverter classes.

  • Loading branch information...
1 parent 302e854 commit 42e7f4d103ff11e5e8b86400d68458ba427bfaf8 Hugo Hamon committed May 11, 2012
@@ -69,10 +69,10 @@ public function setBuildProperties($props)
$this->buildProperties = array();
foreach ($props as $key => $propValue) {
- if (strpos($key, "propel.") === 0) {
- $newKey = substr($key, strlen("propel."));
+ if (0 === strpos($key, 'propel.')) {
+ $newKey = substr($key, strlen('propel.'));
$j = strpos($newKey, '.');
- while ($j !== false) {
+ while (false !== $j) {
$newKey = substr($newKey, 0, $j) . ucfirst(substr($newKey, $j + 1));
$j = strpos($newKey, '.');
}
@@ -120,7 +120,7 @@ public function getClassName($propname)
// This is a slight hack to workaround camel case inconsistencies for the DataSQL classes.
// Basically, we want to turn ?.?.?.sqliteDataSQLBuilder into ?.?.?.SqliteDataSQLBuilder
$lastdotpos = strrpos($classpath, '.');
- if ($lastdotpos !== false) {
+ if (false !== $lastdotpos) {
$classpath{$lastdotpos+1} = strtoupper($classpath{$lastdotpos+1});
} else {
// Allows to configure full classname instead of a dot-path notation
@@ -277,20 +277,22 @@ protected function parseBuildConnections($xmlString)
public function getBuildConnection($databaseName = null)
{
$connections = $this->getBuildConnections();
+
if (null === $databaseName) {
$databaseName = $this->defaultBuildConnection;
}
+
if (isset($connections[$databaseName])) {
return $connections[$databaseName];
- } else {
- // fallback to the single connection from build.properties
- return array(
- 'adapter' => $this->getBuildProperty('databaseAdapter'),
- 'dsn' => $this->getBuildProperty('databaseUrl'),
- 'user' => $this->getBuildProperty('databaseUser'),
- 'password' => $this->getBuildProperty('databasePassword'),
- );
}
+
+ // fallback to the single connection from build.properties
+ return array(
+ 'adapter' => $this->getBuildProperty('databaseAdapter'),
+ 'dsn' => $this->getBuildProperty('databaseUrl'),
+ 'user' => $this->getBuildProperty('databaseUser'),
+ 'password' => $this->getBuildProperty('databasePassword'),
+ );
}
public function getBuildPDO($database)
@@ -85,6 +85,49 @@ protected function setupObject()
$this->defaultStringFormat = $this->getAttribute('defaultStringFormat', 'YAML');
}
+ public function loadDefinition(array $definition)
+ {
+ if (!empty($definition['name'])) {
+ $this->name = $definition['name'];
+ }
+
+ if (!empty($definition['defaultIdMethod'])) {
+ $this->defaultIdMethod = $definition['defaultIdMethod'];
+ }
+
+ if (!empty($definition['package'])) {
+ $this->setPackage($definition['package']);
+ }
+
+ if (!empty($definition['schema'])) {
+ $this->schema = $definition['schema'];
+ }
+
+ if (!empty($definition['namespace'])) {
+ $this->namespace = $definition['namespace'];
+ }
+
+ if (!empty($definition['baseClass'])) {
+ $this->baseClass = $definition['baseClass'];
+ }
+
+ if (!empty($definition['basePeer'])) {
+ $this->basePeer = $definition['basePeer'];
+ }
+
+ if (!empty($definition['defaultPhpNamingMethod'])) {
+ $this->defaultPhpNamingMethod = $definition['defaultPhpNamingMethod'];
+ }
+
+ if (!empty($definition['heavyIndexing'])) {
+ $this->heavyIndexing = (boolean) $definition['heavyIndexing'];
+ }
+
+ if (!empty($definition['tablePrefix'])) {
+ $this->tablePrefix = $definition['tablePrefix'];
+ }
+ }
+
/**
* Returns the PlatformInterface implementation for this database.
*
@@ -134,7 +134,7 @@ public function getName()
*/
public function getShortName()
{
- return str_replace("-schema", "", $this->name);
+ return str_replace('-schema', '', $this->name);
}
/**
@@ -310,8 +310,8 @@ public function countTables()
public function toString()
{
$result = '<app-data>'."\n";
- foreach ($this->databases as $dbList) {
- $result .= $dbList->toString();
+ foreach ($this->databases as $database) {
+ $result .= $database->toString();
}
if ($this->databases) {
@@ -331,4 +331,15 @@ public function __toString()
{
return $this->toString();
}
+
+ public function loadDefinition(array $databases)
+ {
+ foreach ($databases as $database) {
+ $db = new Database();
+ $db->setParentSchema($this);
+ $db->loadDefinition($database);
+
+ $this->addDatabase($db);
+ }
+ }
}
@@ -0,0 +1,20 @@
+<?php
+
+namespace Propel\Generator\Schema;
+
+interface SchemaConverterInterface
+{
+ /**
+ * Parses a schema definition and returns an array.
+ *
+ * @return array
+ */
+ function getArrayDefinition($file);
+
+ /**
+ * Returns whether or not the converter supports a specific schema file.
+ *
+ * @return Boolean
+ */
+ function supports($file);
+}
@@ -0,0 +1,96 @@
+<?php
+
+namespace Propel\Generator\Schema;
+
+use Propel\Generator\Model\Schema;
+
+class XmlSchemaConverter implements SchemaConverterInterface
+{
+ private $definition;
+
+ public function __construct()
+ {
+ $this->definition = array();
+ }
+
+ public function getArrayDefinition($file)
+ {
+ if (!file_exists($file)) {
+ throw new \InvalidArgumentException(sprintf('Unable to load schema file for path "%s".', $file));
+ }
+
+ //$doc = new \DOMDocument();
+ //$doc->loadXML(file_get_contents($file));
+
+ //return $this->xmlToArray($doc->documentElement);
+
+ return $this->toArray(file_get_contents($file));
+ }
+
+ public function supports($file)
+ {
+ return 'xml' === pathinfo($file, PATHINFO_EXTENSION);
+ }
+
+ private function parseNode(\SimpleXmlElement $node)
+ {
+ $output = $this->extractNodeAttributes($node);
+
+ foreach ($node->children() as $child) {
+ $name = $child->getName();
+ $key = $this->getParentKey($name);
+ $definition = $this->parseNode($child);
+
+ if (null === $key) {
+ $output[$name] = $definition;
+ } else {
+ $output[$key][] = $definition;
+ }
+ }
+
+ return $output;
+ }
+
+ private function getParentKey($childName)
+ {
+ $map = array(
+ 'table' => 'tables',
+ 'column' => 'columns',
+ 'foreign-key' => 'foreign-keys',
+ 'index' => 'indices',
+ 'index-column' => 'index-columns',
+ 'unique' => 'uniques',
+ 'external-schema' => 'external-schemas',
+ 'id-method-parameter' => 'id-method-parameters',
+ 'parameter' => 'parameters',
+ );
+
+ return isset($map[$childName]) ? $map[$childName] : null;
+ }
+
+ private function toArray($xml)
+ {
+ $root = new \SimpleXmlElement($xml);
+
+ $definition = $this->parseNode($root);
+ if (!isset($definition['external-schemas'])) {
+ return array($definition);
+ }
+
+ $definitions = array();
+
+ // Parse external schemas definitions...
+
+ return $definitions;
+ }
+
+ private function extractNodeAttributes(\SimpleXmlElement $node)
+ {
+ $attributes = array();
+ foreach ($node->attributes() as $name => $value) {
+ $attributes[$name] = (string) $value;
+ }
+
+ return $attributes;
+ }
+}
@@ -0,0 +1,21 @@
+<database
+ name="bookstore"
+ defaultIdMethod="native"
+ package="Bookstore"
+ schema="bookstore"
+ namespace="Store\Book"
+ baseClass="BaseObject"
+ basePeer="BasePeer"
+ heavyIndexing="true"
+ tablePrefix="propel_"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:noNamespaceSchemaLocation="http://xsd.propelorm.org/1.6/database.xsd">
+
+ <table name="book" description="Book Table">
+ <column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" description="Book Id" />
+ <column name="title" type="VARCHAR" required="true" description="Book Title" primaryString="true" />
+ <column name="isbn" required="true" type="VARCHAR" size="24" phpName="ISBN" description="ISBN Number" />
+ <column name="price" required="false" type="FLOAT" description="Price of the book." />
+ </table>
+
+</database>
@@ -31,7 +31,7 @@ public function testMakeList()
{
$expected = 'Column0, Column1, Column2, Column3, Column4';
$objArray = array();
- for ($i=0; $i<5; $i++) {
+ for ($i = 0; $i < 5; $i++) {
$c = new Column();
$c->setName("Column" . $i);
$objArray[] = $c;
@@ -41,7 +41,7 @@ public function testMakeList()
$this->assertEquals($expected, $list, sprintf("Expected '%s' match, got '%s' ", var_export($expected, true), var_export($list,true)));
$strArray = array();
- for ($i=0; $i<5; $i++) {
+ for ($i = 0; $i < 5; $i++) {
$strArray[] = "Column" . $i;
}
@@ -14,7 +14,6 @@
use Propel\Tests\Helpers\DummyPlatforms;
use Propel\Tests\Helpers\NoSchemaPlatform;
use Propel\Tests\Helpers\SchemaPlatform;
-
use Propel\Generator\Model\Database;
use Propel\Generator\Model\Table;
@@ -35,6 +34,39 @@ public function providerForTestHasTable()
);
}
+ public function testLoadDefinition()
+ {
+ $definition = array(
+ 'name' => 'bookstore',
+ 'defaultIdMethod' => 'native',
+ 'package' => 'Foo',
+ 'schema' => 'acme',
+ 'namespace' => 'Acme',
+ 'baseClass' => 'CustomRecord',
+ 'basePeer' => 'CustomPeer',
+ 'defaultPhpNamingMethod' => 'phpname',
+ 'heavyIndexing' => 'true',
+ 'tablePrefix' => 'bs_',
+ );
+
+ $database = new Database();
+ $database->setSchema('book');
+ $database->setPackage('Acme');
+ $database->setNamespace('Acme\Model');
+ $database->loadDefinition($definition);
+
+ $this->assertEquals('bookstore', $database->getName());
+ $this->assertEquals('native', $database->getDefaultIdMethod());
+ $this->assertEquals('Foo', $database->getPackage());
+ $this->assertEquals('acme', $database->getSchema());
+ $this->assertEquals('Acme', $database->getNamespace());
+ $this->assertEquals('CustomRecord', $database->getBaseClass());
+ $this->assertEquals('CustomPeer', $database->getBasePeer());
+ $this->assertEquals('phpname', $database->getDefaultPhpNamingMethod());
+ $this->assertEquals('bs_', $database->getTablePrefix());
+ $this->assertTrue($database->getHeavyIndexing());
+ }
+
public function testTableInheritsSchema()
{
$database = new Database();
@@ -0,0 +1,24 @@
+<?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
+ */
+
+use Propel\Generator\Schema\XmlSchemaConverter;
+
+class XmlSchemaConverterTest extends \PHPUnit_Framework_TestCase
+{
+ public function testGetArrayDefinition()
+ {
+ $file = __DIR__.'/../../../../Fixtures/full-schema.xml';
+
+ $converter = new XmlSchemaConverter();
+ $definition = $converter->getArrayDefinition($file);
+
+ $this->assertCount(1, $definition[0]['tables']);
+ }
+}

0 comments on commit 42e7f4d

Please sign in to comment.