Skip to content

Commit

Permalink
Merge pull request #2 from diabolo-framework/master
Browse files Browse the repository at this point in the history
.
  • Loading branch information
Michael Luthor authored Sep 24, 2018
2 parents d3ebf86 + 1f9c1b1 commit f5bfbb3
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Framework/Core/Component/Stringx.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ public static function snakeToCamel( $string ) {
return implode('', ucwords(str_replace('_', ' ', $string)));
}

/**
* convert snake case to camel case
* @param unknown $string
* @return string
*/
public static function middleSnakeToCamel( $string ) {
return str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));
}

/**
* convert camel case to snake case
* @param unknown $string
Expand Down
1 change: 1 addition & 0 deletions Framework/Service/Action/ActionGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function runAction( $name, $params=array() ) {
if ( isset($this->registeredActions[$name]) ) {
$actionClassName = $this->registeredActions[$name];
} else {
$name = implode('\\', array_map('ucfirst', explode('/', $name)));
$actionClassName = $this->namespace.'\\'.Stringx::middleSnakeToCamel($name);
}

Expand Down
5 changes: 5 additions & 0 deletions Framework/Service/Database/Command/Migration/Up.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function run( $args=array() ) {
'processHandler' => array($this, 'processHandler'),
'history' => array(
'class' => File::class,
'path' => (isset($args['options']['history-path'])) ? $args['options']['history-path'] : null,
),
));

Expand Down Expand Up @@ -42,7 +43,11 @@ public function processHandler( $name, $process ) {
case 'DropIndex' : echo " => drop index : {$process['tableName']}.{$process['indexName']}\n"; break;
case 'AddForginKey' : echo " => add forgin key : {$process['tableName']}.{$process['fkName']}\n"; break;
case 'DropForginKey' : echo " => drop forgin key : {$process['tableName']}.{$process['fkName']}\n"; break;
case 'DeleteData' : echo " => delete data : {$process['tableName']} [{$process['count']} row(s)] - {$process['message']}\n"; break;
case 'UpdateData' : echo " => update data : {$process['tableName']} [{$process['count']} row(s)] - {$process['message']}\n"; break;
case 'Message' : echo " => [msg] : {$process['text']} \n"; break;
case 'DoneMigration' : echo "\n\ndone migration : {$process['count']} executed.\n"; break;
case 'Error' : echo "\n\nmigration failed : {$process['message']}\nLocation:{$process['file']}#{$process['line']}\n"; exit();
default : echo "unknown process action `{$name}`\n"; break;
}
}
Expand Down
12 changes: 10 additions & 2 deletions Framework/Service/Database/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ public function up( $step=0 ) {
$migration = new $migrationClass();
$migration->setProcessHandler(array($this, 'processHandler'));
$this->processHandler('StartMigration',array('name'=>$name));
$migration->up();
try {
$migration->up();
} catch ( \Exception $e ) {
$this->processHandler('Error',array('message'=>$e->getMessage(), 'file'=>$e->getFile(), 'line'=>$e->getLine()));
}
$this->history->add($name);
}
$this->history->save();
Expand Down Expand Up @@ -94,7 +98,11 @@ public function down( $step=0 ) {
$migration = new $migrationClass();
$migration->setProcessHandler(array($this, 'processHandler'));
$this->processHandler('StartMigration',array('name'=>$name));
$migration->down();
try {
$migration->down();
} catch ( \Exception $e ) {
$this->processHandler('Error',array('message'=>$e->getMessage(), 'file'=>$e->getFile(), 'line'=>$e->getLine()));
}
$this->history->drop($name);
}
$this->history->save();
Expand Down
6 changes: 5 additions & 1 deletion Framework/Service/Database/Migration/HistoryHandler/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ class File implements DatabaseMigrationHistoryHandler {
* @param unknown $config
*/
public function __construct( $config ) {
$this->path = $config['path']."/.history.php";
if ( !is_dir($config['path']) ) {
$this->path = $config['path'];
} else {
$this->path = rtrim($config['path'], DIRECTORY_SEPARATOR."/")."/.history.php";
}
if ( file_exists($this->path) ) {
$this->history = require $this->path;
}
Expand Down
39 changes: 39 additions & 0 deletions Framework/Service/Database/Migration/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use X\Service\Database\Database;
use X\Service\Database\DatabaseException;
use X\Service\Database\Table;
use X\Service\Database\Query;
abstract class Migration {
/** @var callable */
private $processHandler = null;
Expand Down Expand Up @@ -187,4 +188,42 @@ protected function dropForginKey( $tableName, $fkName ) {
'fkName' => $fkName,
));
}

/**
* @param unknown $tableName
* @param unknown $condition
* @param string $message
*/
protected function delete( $tableName, $condition=null, $message='' ) {
$count = Query::delete($this->getDb())->from($tableName)->where($condition)->exec();
$this->processHandler('DeleteData',array(
'tableName' => $tableName,
'message' => $message,
'count' => $count,
));
}

/**
* @param unknown $tableName
* @param unknown $data
* @param unknown $condition
* @param unknown $message
*/
protected function update( $tableName, $data, $condition=null, $message='' ) {
$count = Query::update($this->getDb())->table($tableName)->values($data)->where($condition)->exec();
$this->processHandler('UpdateData',array(
'tableName' => $tableName,
'message' => $message,
'count' => $count,
));
}

/**
* @param unknown $text
*/
protected function message( $text ) {
$this->processHandler('Message', array(
'text' => $text,
));
}
}

0 comments on commit f5bfbb3

Please sign in to comment.