Skip to content

Commit

Permalink
SchemaUtils refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jan 26, 2016
1 parent 91256f1 commit 24cc2a8
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 41 deletions.
18 changes: 0 additions & 18 deletions src/LazyRecord/ClassUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,6 @@ static public function get_declared_schema_classes()
return self::filter_schema_classes($classes);
}

/**
* Get referenced schema classes and put them in order.
*
* @param string[] schema objects
*/
static public function expand_schema_classes(array $classes)
{
$schemas = array();
foreach ($classes as $class) {
$schema = new $class; // declare schema
$refs = $schema->getReferenceSchemas();
foreach ( $refs as $refClass => $v ) {
$schemas[] = $refClass;
}
$schemas[] = $class;
}
return self::schema_classes_to_objects(array_unique($schemas));
}


public static function schema_classes_to_objects(array $classes)
Expand Down
5 changes: 2 additions & 3 deletions src/LazyRecord/Command/BasedataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use LazyRecord\ClassUtils;
use LazyRecord\SeedBuilder;
use LazyRecord\Schema\SchemaUtils;
use Exception;
use LazyRecord\Schema\SchemaCollection;
use Exception;

class BaseDataCommand extends BaseCommand
{
Expand All @@ -19,8 +19,7 @@ public function execute()
$options = $this->options;
$logger = $this->logger;


$classes = $this->findSchemasByArguments( func_get_args() );
$classes = SchemaUtils::findSchemasByArguments($this->getConfigLoader(), func_get_args(), $this->logger);

SchemaUtils::printSchemaClasses($classes, $this->logger);

Expand Down
2 changes: 1 addition & 1 deletion src/LazyRecord/Command/SchemaCommand/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function execute()
$config = $this->getConfigLoader();

$this->logger->debug('Finding schemas...');
$schemas = $this->findSchemasByArguments(func_get_args());
$schemas = SchemaUtils::findSchemasByArguments($this->getConfigLoader(), func_get_args(), $this->logger);

$generator = new SchemaGenerator($config);
if ($this->options->force) {
Expand Down
3 changes: 2 additions & 1 deletion src/LazyRecord/Command/SchemaCommand/CleanCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace LazyRecord\Command\SchemaCommand;
use LazyRecord\Schema\SchemaGenerator;
use LazyRecord\Schema\SchemaUtils;
use LazyRecord\Command\BaseCommand;

/**
Expand Down Expand Up @@ -33,7 +34,7 @@ public function execute()
$config = $this->getConfigLoader();

$this->logger->debug('Finding schemas...');
$schemas = $this->findSchemasByArguments( func_get_args() );
$schemas = SchemaUtils::findSchemasByArguments($this->getConfigLoader(), func_get_args(), $this->logger);

foreach ($schemas as $schema) {
$this->logger->info('Cleaning schema ' . get_class($schema) );
Expand Down
2 changes: 1 addition & 1 deletion src/LazyRecord/Command/SchemaCommand/StatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function execute()
$actionLogger = new ActionLogger(STDERR);


$schemas = $this->findSchemasByArguments(func_get_args());
$schemas = SchemaUtils::findSchemasByArguments($this->getConfigLoader(), func_get_args(), $this->logger);
foreach ($schemas as $schema) {

if ($this->logger->isVerbose()) {
Expand Down
5 changes: 2 additions & 3 deletions src/LazyRecord/Command/SqlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use SQLBuilder\Driver\MySQLDriver;
use SQLBuilder\Driver\PgSQLDriver;
use SQLBuilder\Driver\SQLiteDriver;
use LazyRecord\Schema\SchemaUtils;
use Exception;

class SqlCommand extends BaseCommand
Expand Down Expand Up @@ -59,12 +60,10 @@ public function execute()
$id = $this->getCurrentDataSourceId();

$logger->debug("Finding schema classes...");
$schemas = $this->findSchemasByArguments(func_get_args());
$schemas = SchemaUtils::findSchemasByArguments($this->getConfigLoader(), func_get_args(), $this->logger);

$logger->debug("Initialize schema builder...");



if ($output = $this->options->output) {
$configLoader = $this->getConfigLoader(true);
$dataSourceConfig = $configLoader->getDataSource($id);
Expand Down
25 changes: 11 additions & 14 deletions src/LazyRecord/Schema/SchemaBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,42 +154,39 @@ public function getRelations()
*/
public function getReferenceSchemas($recursive = true)
{
$schemas = array();
foreach( $this->relations as $rel ) {
if ( ! isset($rel['foreign_schema']) ) {
$schemas = [];
foreach ($this->relations as $relKey => $rel ) {
if (!isset($rel['foreign_schema'])) {
continue;
}

$class = ltrim($rel['foreign_schema'],'\\');

if ( isset($schemas[$class]) ) {
if (isset($schemas[$class]) ) {
continue;
}

if (! class_exists($class,true)) {
throw new RuntimeException("Foreign schema class $class not found." );
}

if ( is_a($class,'LazyRecord\\BaseModel',true) ) {

if (is_a($class,'LazyRecord\\BaseModel',true)) {
// bless model class to schema object.
if ( ! method_exists($class, 'schema') ) {
throw new Exception( get_class($this) . ": You need to define schema method in $class class.");
if (! method_exists($class, 'schema')) {
throw new Exception(get_class($this) . ": You need to define schema method in $class class.");
}
$schemas[ $class ] = 1;
$model = new $class;
$schema = new DynamicSchemaDeclare($model);
if( $recursive ) {
$schemas = array_merge($schemas, $schema->getReferenceSchemas(false));
}
}
elseif ( is_subclass_of($class, 'LazyRecord\\Schema\\DeclareSchema',true ) ) {
} else if (is_subclass_of($class, 'LazyRecord\\Schema\\DeclareSchema',true)) {
$schemas[ $class ] = 1;
$fs = new $class;
if( $recursive ) {
if ($recursive) {
$schemas = array_merge($schemas, $fs->getReferenceSchemas(false));
}
}
else {
} else {
throw new InvalidArgumentException("Foreign schema class $class is not a SchemaDeclare class");
}

Expand Down
27 changes: 27 additions & 0 deletions src/LazyRecord/Schema/SchemaUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ static public function printSchemaClasses(array $classes, Logger $logger = NULL)
}
}

/*
static public function find_schema_parents(array $classes)
{
$parents = [];
foreach ($classes as $class) {
$schema = new $class; // declare schema
foreach ($schema->relations as $relKey => $rel ) {
if (!isset($rel['foreign_schema'])) {
continue;
}
$foreignClass = ltrim($rel['foreign_schema'],'\\');
$schema = new $foreignClass;
if ($rel->type == Relationship::BELONGS_TO) {
$parents[$class][] = $foreignClass;
} else if ($rel->type == Relationship::HAS_ONE || $rel->type == Relationship::HAS_MANY) {
$parents[$foreignClass][] = $class;
}
}
}
return $parents;
}
*/


/**
* Get referenced schema classes and put them in order.
Expand All @@ -35,6 +58,10 @@ static public function printSchemaClasses(array $classes, Logger $logger = NULL)
*/
static public function expandSchemaClasses(array $classes)
{




$map = array();
$schemas = array();
foreach ($classes as $class) {
Expand Down

0 comments on commit 24cc2a8

Please sign in to comment.