aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilly Micieli <micieli@laposte.net>2020-02-18 22:53:56 +0000
committerWilly Micieli <micieli@laposte.net>2020-02-18 22:53:56 +0000
commitce21315ae5432e5f7748c19d55f1d8d1f796c11d (patch)
tree5a1507641665d7ed0a4d412f29397a7f76d279ea
parentc575793ad05ed271e10f0d81a40b82205c59a996 (diff)
downloadeywa-ikran.zip
eywa-ikran.tar.gz
clean code fix bugsikrandevelop
-rw-r--r--config/connexion.yaml (renamed from config/connection.yaml)0
-rw-r--r--eywa/Application/App.php4
-rw-r--r--eywa/Console/Database/CleanDatabase.php19
-rw-r--r--eywa/Console/Database/CreateDatabase.php13
-rw-r--r--eywa/Console/Database/CreateMigrationTable.php20
-rw-r--r--eywa/Console/Database/CreateUser.php13
-rw-r--r--eywa/Console/Database/DropTable.php108
-rw-r--r--eywa/Console/Database/MigrateDatabase.php8
-rw-r--r--eywa/Console/Database/ShowTable.php15
-rw-r--r--eywa/Console/Database/TruncateTable.php17
-rw-r--r--eywa/Database/Base/Base.php47
-rw-r--r--eywa/Database/Connexion/Connect.php10
-rw-r--r--eywa/Database/Connexion/Connexion.php3
-rw-r--r--eywa/Database/Migration/Migrate.php276
-rw-r--r--eywa/Database/Migration/Migration.php65
-rw-r--r--eywa/Database/Seed/Seeder.php4
-rw-r--r--eywa/Database/Table/Table.php8
-rw-r--r--eywa/Ioc/Container.php3
-rw-r--r--helpers/admin.php42
-rwxr-xr-x[-rw-r--r--]shaolin7
-rw-r--r--tests/App/ApplicationTest.php4
-rw-r--r--web/index.php2
22 files changed, 475 insertions, 213 deletions
diff --git a/config/connection.yaml b/config/connexion.yaml
index 16d9d9a..16d9d9a 100644
--- a/config/connection.yaml
+++ b/config/connexion.yaml
diff --git a/eywa/Application/App.php b/eywa/Application/App.php
index 4a2ad8c..39132c9 100644
--- a/eywa/Application/App.php
+++ b/eywa/Application/App.php
@@ -12,7 +12,6 @@ namespace Eywa\Application {
use Eywa\Cache\RedisCache;
use Eywa\Collection\Collect;
use Eywa\Configuration\Config;
- use Eywa\Database\Connexion\Connect;
use Eywa\Database\Connexion\Connexion;
use Eywa\Database\Query\Sql;
use Eywa\Detection\Detect;
@@ -407,8 +406,7 @@ namespace Eywa\Application {
*/
public function connexion(): Connexion
{
- $prod = new Connect($this->env('DB_DRIVER','mysql'),$this->env('DB_NAME','eywa'),$this->env('DB_USERNAME','eywa'),$this->env('DB_PASSWORD','eywa'),intval($this->env('DB_PORT',3306)),$this->config('connection','options'),$this->env('DB_HOST','localhost'));
- return equal($this->config('mode','connexion'),'prod') ? $prod : $prod->development();
+ return equal($this->config('mode','connexion'),'prod') ? production() : development();
}
/**
diff --git a/eywa/Console/Database/CleanDatabase.php b/eywa/Console/Database/CleanDatabase.php
index 27ba685..d440fa0 100644
--- a/eywa/Console/Database/CleanDatabase.php
+++ b/eywa/Console/Database/CleanDatabase.php
@@ -6,7 +6,9 @@ namespace Eywa\Console\Database {
use Eywa\Database\Base\Base;
+ use Eywa\Exception\Kedavra;
use Symfony\Component\Console\Command\Command;
+ use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
@@ -17,13 +19,26 @@ namespace Eywa\Console\Database {
protected function configure()
{
- $this->setDescription("Truncate all tables");
+ $this->setDescription("Truncate all tables")->addArgument('env',InputArgument::REQUIRED,'The base environment');
}
+ /**
+ * @param InputInterface $input
+ * @param OutputInterface $output
+ * @return int
+ * @throws Kedavra
+ */
public function execute(InputInterface $input, OutputInterface $output)
{
+ $env = $input->getArgument('env');
+
+ not_in(['dev','prod','any'],$env,true,"Only dev, prod or any must be used");
+
$io = new SymfonyStyle($input,$output);
- if((new Base())->clean())
+
+
+
+ if((new Base($env))->clean())
{
$io->success('All tables are now empty');
return 0;
diff --git a/eywa/Console/Database/CreateDatabase.php b/eywa/Console/Database/CreateDatabase.php
deleted file mode 100644
index 5dfe44b..0000000
--- a/eywa/Console/Database/CreateDatabase.php
+++ /dev/null
@@ -1,13 +0,0 @@
-<?php
-
-
-namespace Eywa\Console\Database {
-
-
- use Symfony\Component\Console\Command\Command;
-
- class CreateDatabase extends Command
- {
-
- }
-} \ No newline at end of file
diff --git a/eywa/Console/Database/CreateMigrationTable.php b/eywa/Console/Database/CreateMigrationTable.php
index b9e949e..d1fedf6 100644
--- a/eywa/Console/Database/CreateMigrationTable.php
+++ b/eywa/Console/Database/CreateMigrationTable.php
@@ -22,14 +22,24 @@ namespace Eywa\Console\Database {
public function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input,$output);
- if((new \Eywa\Database\Migration\CreateMigrationTable())->up())
+ if((new \Eywa\Database\Migration\CreateMigrationTable(production()))->up())
{
- $io->success('The migration table has been created successfully');
+ $io->success('The production migration table has been created successfully');
- return 0;
+ }else{
+ $io->error('The migrations table generation has failed');
+ return 1;
}
- $io->error('The migrations table generation has failed');
- return 1;
+ if((new \Eywa\Database\Migration\CreateMigrationTable(development()))->up())
+ {
+ $io->success('The development migration table has been created successfully');
+
+ }else{
+ $io->error('The migrations table generation has failed');
+ return 1;
+
+ }
+ return 0;
}
}
} \ No newline at end of file
diff --git a/eywa/Console/Database/CreateUser.php b/eywa/Console/Database/CreateUser.php
deleted file mode 100644
index 08b77fc..0000000
--- a/eywa/Console/Database/CreateUser.php
+++ /dev/null
@@ -1,13 +0,0 @@
-<?php
-
-
-namespace Eywa\Console\Database {
-
-
- use Symfony\Component\Console\Command\Command;
-
- class CreateUser extends Command
- {
-
- }
-} \ No newline at end of file
diff --git a/eywa/Console/Database/DropTable.php b/eywa/Console/Database/DropTable.php
index d526280..0ddd014 100644
--- a/eywa/Console/Database/DropTable.php
+++ b/eywa/Console/Database/DropTable.php
@@ -17,36 +17,116 @@
protected function configure()
{
- $this->setDescription("Drop a table")->addArgument('table',InputArgument::REQUIRED,'The table name');
+ $this->setDescription("Drop a table")->addArgument('table',InputArgument::REQUIRED,'The table name')->addArgument('env',InputArgument::REQUIRED,'The base environment');
}
public function execute(InputInterface $input, OutputInterface $output)
{
+ $env = $input->getArgument('env');
+
+ not_in(['dev','prod','any'],$env,true,"Only dev, prod or any must be used");
+
$io = new SymfonyStyle($input,$output);
$x = $input->getArgument('table');
- $table = new Table();
-
- if (not_def($table->show()))
+ if (equal($env, 'dev'))
{
- $io->error("No tables found");
- return 1;
- }
+ $table = new Table(development());
- if (is_false($table->exist($x)))
+ if (not_def($table->show()))
+ {
+ $io->error("No tables found");
+ return 1;
+ }
+
+ if (is_false($table->exist($x)))
+ {
+ $io->error("The $x table not exist");
+ return 1;
+ }
+ if ($table->from($x)->drop())
+ {
+ $io->success("The $x table was removed successfully");
+ return 0;
+ }else{
+ $io->error("Failed to remove the $x table");
+ return 1;
+ }
+ }
+ if (equal($env, 'prod'))
{
- $io->error("The $x table not exist");
- return 1;
+ $table = new Table(production());
+
+ if (not_def($table->show()))
+ {
+ $io->error("No tables found");
+ return 1;
+ }
+
+ if (is_false($table->exist($x)))
+ {
+ $io->error("The $x table not exist");
+ return 1;
+ }
+ if ($table->from($x)->drop())
+ {
+ $io->success("The $x table was removed successfully");
+ return 0;
+ }else{
+ $io->error("Failed to remove the $x table");
+ return 1;
+ }
}
- if ($table->from($x)->drop())
+ if (equal($env, 'any'))
{
- $io->success("The $x table was removed successfully");
- return 0;
+ $table = new Table(development());
+
+ if (not_def($table->show()))
+ {
+ $io->error("No tables found");
+ return 1;
+ }
+
+ if (is_false($table->exist($x)))
+ {
+ $io->error("The $x table not exist");
+ return 1;
+ }
+ if ($table->from($x)->drop())
+ {
+ $io->success("The $x table was removed successfully");
+ return 0;
+ }else{
+ $io->error("Failed to remove the $x table");
+ return 1;
+ }
+
+ $table = new Table(production());
+
+ if (not_def($table->show()))
+ {
+ $io->error("No tables found");
+ return 1;
+ }
+
+ if (is_false($table->exist($x)))
+ {
+ $io->error("The $x table not exist");
+ return 1;
+ }
+ if ($table->from($x)->drop())
+ {
+ $io->success("The $x table was removed successfully");
+ return 0;
+ }else{
+ $io->error("Failed to remove the $x table");
+ return 1;
+ }
}
- $io->error("Fail to remove the $x table");
+ $io->error("Environment not valid");
return 1;
}
diff --git a/eywa/Console/Database/MigrateDatabase.php b/eywa/Console/Database/MigrateDatabase.php
index 9cbebce..7a76cd0 100644
--- a/eywa/Console/Database/MigrateDatabase.php
+++ b/eywa/Console/Database/MigrateDatabase.php
@@ -5,7 +5,6 @@ namespace Eywa\Console\Database {
use Eywa\Database\Migration\Migrate;
- use Eywa\Time\Timing;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
@@ -37,7 +36,12 @@ namespace Eywa\Console\Database {
$i = 0;
do{
- Migrate::run('up',$io);
+ if(Migrate::run('up',$io) !== 0)
+ {
+ $io->error('A migration has fail');
+ die();
+ }
+
$i++;
}while($i!==$end);
$io->success('All migration has been executed successfully');
diff --git a/eywa/Console/Database/ShowTable.php b/eywa/Console/Database/ShowTable.php
index eb3b997..84301ca 100644
--- a/eywa/Console/Database/ShowTable.php
+++ b/eywa/Console/Database/ShowTable.php
@@ -5,6 +5,7 @@
use Eywa\Database\Table\Table;
use Symfony\Component\Console\Command\Command;
+ use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
@@ -16,15 +17,25 @@
protected function configure()
{
- $this->setDescription("List all tables found in the base");
+ $this->setDescription("List all tables found in the base")->addArgument('env',InputArgument::REQUIRED,'The base environment');
}
public function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input,$output);
+ $env = $input->getArgument('env');
- $x = (new Table())->show();
+ not_in(['dev','prod'],$env,true,'The env must be dev or prod');
+
+ if (equal($env,'dev'))
+ {
+ $x = (new Table(development()))->show();
+
+ }else{
+ $x = (new Table(production()))->show();
+
+ }
if (not_def($x))
{
$io->error("No tables found");
diff --git a/eywa/Console/Database/TruncateTable.php b/eywa/Console/Database/TruncateTable.php
index 0dfa4e4..156daed 100644
--- a/eywa/Console/Database/TruncateTable.php
+++ b/eywa/Console/Database/TruncateTable.php
@@ -18,15 +18,26 @@
protected function configure()
{
- $this->setDescription("Truncate a table")->addArgument('table',InputArgument::REQUIRED,'The table name');
+ $this->setDescription("Truncate a table")->addArgument('table',InputArgument::REQUIRED,'The table name')->addArgument('env',InputArgument::REQUIRED,'The base environment');
}
public function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input,$output);
- $table = $input->getArgument('table');
- $success = (new Table())->from($table)->truncate();
+ $env = $input->getArgument('env');
+ $table = $input->getArgument('table');
+ not_in(['dev','prod'],$env,true,'The environement used is not valid');
+
+ if (equal($env,'dev'))
+ {
+ $success = (new Table(development()))->from($table)->truncate();
+ }else{
+
+ $success = (new Table(production()))->from($table)->truncate();
+ }
+
+
if ($success)
{
$io->success("The $table table has been successfully truncated");
diff --git a/eywa/Database/Base/Base.php b/eywa/Database/Base/Base.php
index 66da23a..f5ab9ad 100644
--- a/eywa/Database/Base/Base.php
+++ b/eywa/Database/Base/Base.php
@@ -6,28 +6,61 @@ declare(strict_types=1);
namespace Eywa\Database\Base {
use Eywa\Database\Table\Table;
+ use Eywa\Exception\Kedavra;
class Base
{
+
/**
- * @var Table
+ * @var string
*/
- private Table $table;
+ private string $env;
- public function __construct()
+ /**
+ * Base constructor.
+ * @param string $env
+ */
+ public function __construct(string $env)
{
- $this->table = new Table();
+ $this->env = $env;
+
}
+ /**
+ * @return bool
+ * @throws Kedavra
+ */
public function clean()
{
- foreach ($this->table->show() as $table)
+ if (equal($this->env,'dev'))
{
- if (different($table,'migrations'))
- is_false((new Table())->from($table)->truncate(),true,"Error");
+ foreach ((new Table(development()))->show() as $table)
+ if (different($table,'migrations'))
+ is_false((new Table(development()))->from($table)->truncate(),true,"Failed to truuncate the $table table");
+ return true;
+ }
+ if (equal($this->env,'prod'))
+ {
+ foreach ((new Table(production()))->show() as $table)
+ if (different($table,'migrations'))
+ is_false((new Table(production()))->from($table)->truncate(),true,"Failed to truuncate the $table table");
+ return true;
+ }
+
+ if (equal($this->env,'any'))
+ {
+ foreach ((new Table(production()))->show() as $table)
+ if (different($table,'migrations'))
+ is_false((new Table(production()))->from($table)->truncate(),true,"Failed to truuncate the $table table");
+
+ foreach ((new Table(development()))->show() as $table)
+ if (different($table,'migrations'))
+ is_false((new Table(development()))->from($table)->truncate(),true,"Failed to truuncate the $table table");
+ return true;
}
+
return true;
}
}
diff --git a/eywa/Database/Connexion/Connect.php b/eywa/Database/Connexion/Connect.php
index 78ad30e..639b3db 100644
--- a/eywa/Database/Connexion/Connect.php
+++ b/eywa/Database/Connexion/Connect.php
@@ -86,14 +86,14 @@ namespace Eywa\Database\Connexion {
/**
*
- * @Inject({"db.driver","db.name","db.username", "db.password","db.port","db.options","db.host"})
+ * @Inject({"db.driver","db.name","db.username", "db.password","db.port","db.host"})
*
* @inheritDoc
*
*/
- public function __construct(string $driver, string $base,string $username ='',string $password ='',int $port = 3306, array $options = [], string $host = LOCALHOST)
+ public function __construct(string $driver, string $base,string $username ='',string $password ='',int $port = 3306,string $host = LOCALHOST)
{
- $this->connexion = equal($driver,SQL_SERVER) ? new PDO("$driver:Serve=$host;Database=$base",$username,$password,$options) : (equal($driver,SQLITE) ? new PDO("sqlite:$base") : new PDO("$driver:host=$host;port=$port;dbname=$base", $username, $password, $options));
+ $this->connexion = equal($driver,SQL_SERVER) ? new PDO("$driver:Serve=$host;Database=$base",$username,$password,config('connexion','options')) : (equal($driver,SQLITE) ? new PDO("sqlite:$base") : new PDO("$driver:host=$host;port=$port;dbname=$base", $username, $password, config('connexion','options')));
$this->connexion->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
@@ -109,7 +109,7 @@ namespace Eywa\Database\Connexion {
$this->port = $port;
$this->username = $username;
$this->password = $password;
- $this->options = $options;
+ $this->options = config('connexion','options');
$this->host = $host;
}
@@ -383,7 +383,7 @@ namespace Eywa\Database\Connexion {
*/
public function development(): Connect
{
- return new static(env('DEVELOP_DB_DRIVER','mysql'),env('DEVELOP_DB_NAME','ikran'),env('DEVELOP_DB_USERNAME','ikran'),env('DEVELOP_DB_PASSWORD','ikran'),intval(env('DEVELOP_DB_PORT',3306)),config('connection','options'),env('DEVELOP_DB_HOST','localhost'));
+ return new static(env('DEVELOP_DB_DRIVER','mysql'),env('DEVELOP_DB_NAME','ikran'),env('DEVELOP_DB_USERNAME','ikran'),env('DEVELOP_DB_PASSWORD','ikran'),intval(env('DEVELOP_DB_PORT',3306)),env('DEVELOP_DB_HOST','localhost'));
}
}
} \ No newline at end of file
diff --git a/eywa/Database/Connexion/Connexion.php b/eywa/Database/Connexion/Connexion.php
index acdfb04..f50c466 100644
--- a/eywa/Database/Connexion/Connexion.php
+++ b/eywa/Database/Connexion/Connexion.php
@@ -22,12 +22,11 @@ namespace Eywa\Database\Connexion {
* @param int $port
* @param string $username
* @param string $password
- * @param array $options
* @param string $host
*
* @throws Kedavra
*/
- public function __construct(string $driver, string $base,string $username ='',string $password ='',int $port = 3306, array $options = [], string $host = LOCALHOST);
+ public function __construct(string $driver, string $base,string $username ='',string $password ='',int $port = 3306, string $host = LOCALHOST);
/**
*
* Set the sql queries to execute
diff --git a/eywa/Database/Migration/Migrate.php b/eywa/Database/Migration/Migrate.php
index e43f41c..6126305 100644
--- a/eywa/Database/Migration/Migrate.php
+++ b/eywa/Database/Migration/Migrate.php
@@ -4,6 +4,7 @@
namespace Eywa\Database\Migration {
+ use Eywa\Database\Query\Sql;
use Eywa\Exception\Kedavra;
use Symfony\Component\Console\Style\SymfonyStyle;
@@ -48,9 +49,7 @@ namespace Eywa\Database\Migration {
*/
public static function run(string $mode,SymfonyStyle $io):int
{
- not_in(['up','down'],$mode,true,"The mode must be is up or down");
-
- return equal($mode,'up') ? static::migrate($io,$mode) : static::rollback($io,$mode);
+ return equal($mode,'up') ? static::migrate($io) : static::rollback($io);
}
private static function file(string $class)
@@ -60,69 +59,192 @@ namespace Eywa\Database\Migration {
/**
* @param SymfonyStyle $io
- * @param string $mode
* @return int
* @throws Kedavra
*/
- private static function migrate(SymfonyStyle $io,string $mode = 'up'): int
+ private static function migrate(SymfonyStyle $io): int
+ {
+ static::do('dev','up',$io) ;
+ static::do('prod','up',$io);
+ return 0;
+ }
+
+ /**
+ * @param SymfonyStyle $io
+ * @return int
+ * @throws Kedavra
+ */
+ private static function rollback(SymfonyStyle $io): int
{
+ static::do('dev','down',$io);
+ static::do('prod','down',$io);
+ return 0;
+ }
+ /**
+ * @return bool
+ * @throws Kedavra
+ */
+ public static function check_migrate(): bool
+ {
+ $prod = static::sql('prod');
+ $dev = static::sql('dev');
$return = collect();
- $all = collect(static::list($mode));
- $sql = sql('migrations');
+ foreach (static::list('up') as $class => $date)
+ {
+ $return->push($prod->where('version',EQUAL,$date)->exist() && $dev->where('version',EQUAL,$date)->exist());
+ }
+ return $return->ok();
+ }
+
+
+ /**
+ * @param string $mode
+ * @param string $table
+ * @return Sql
+ * @throws Kedavra
+ */
+ private static function sql(string $mode,string $table = 'migrations'): Sql
+ {
+ return equal($mode,'dev') ? new Sql(development(),$table) : new Sql(production(), $table);
+ }
+
+ /**
+ * @return bool
+ * @throws Kedavra
+ */
+ public static function check_rollback(): bool
+ {
+ return not_def(static::sql('dev')->execute()) && not_def(static::sql('prod')->execute());
+
+ }
+
+ /**
+ * @param string $env
+ * @param string $mode
+ * @param SymfonyStyle $io
+ * @return bool|int
+ * @throws Kedavra
+ */
+ private static function do(string $env,string $mode,SymfonyStyle $io)
+ {
+
+ not_in(['up','down'],$mode,true,"Mode must be up or down");
+ not_in(['dev','prod'],$env,true,"Env must be dev or prod");
+
+ $sql = static::sql($env);
+
+ $all = static::list($mode);
+
+ $result = collect();
if (def($sql->execute()))
{
- foreach ($all->all() as $class => $date)
+ if (equal($mode,'up'))
{
+ foreach ($all as $class => $date)
+ {
- $migration = static::file($class);
+ $migration = static::file($class);
- $created_at = $class::$created_at;
+ $created_at = $class::$created_at;
- $down_success_message = str_replace('%s',$class::$table,$class::$down_success_message);
+ $down_success_message = str_replace('%s',$class::$table,$class::$down_success_message);
- $down_error_message = str_replace('%s',$class::$table,$class::$down_error_message);
+ $down_error_message = str_replace('%s',$class::$table,$class::$down_error_message);
- $up_success_message = str_replace('%s',$class::$table,$class::$up_success_message);
+ $up_success_message = str_replace('%s',$class::$table,$class::$up_success_message);
- $up_error_message = str_replace('%s',$class::$table,$class::$up_error_message);
+ $up_error_message = str_replace('%s',$class::$table,$class::$up_error_message);
- $up_title = str_replace('%s',$class::$table,$class::$up_title);
+ $up_title = str_replace('%s',$class::$table,$class::$up_title);
- $down_title = str_replace('%s',$class::$table,$class::$down_title);
+ $down_title = str_replace('%s',$class::$table,$class::$down_title);
- $exist = $sql->where('version',EQUAL,$date)->exist() && $sql->where('migration',EQUAL,$migration)->exist();
+ $exist = $sql->where('version',EQUAL,$date)->exist();
+ if (!$exist)
+ {
+ $x = equal($env,'prod') ? new $class(production(),$mode,$env) : new $class(development(),$mode,$env);
- if (!$exist)
+ $io->title("$up_title ($env)");
+
+ $result->push(call_user_func_array([$x, $mode], []));
+
+ if ($result->ok())
+ {
+ $io->success($up_success_message);
+ $sql->save(['version'=> $created_at,'migration'=> $migration,'time'=> now()->toDateTimeString()]);
+ return 0;
+ }else
+ {
+
+ $sql->where('version',EQUAL,$date)->destroy();
+ $io->error($up_error_message);
+ return 1;
+ }
+
+ }
+ }
+ }
+
+ if (equal($mode,'down'))
+ {
+ foreach ($all as $class => $date)
{
- $x = new $class;
- $io->title($up_title);
+ $migration = static::file($class);
- $return->push(call_user_func_array([$x, $mode], []));
+ $created_at = $class::$created_at;
- if ($return->ok())
+ $down_success_message = str_replace('%s',$class::$table,$class::$down_success_message);
+
+ $down_error_message = str_replace('%s',$class::$table,$class::$down_error_message);
+
+ $up_success_message = str_replace('%s',$class::$table,$class::$up_success_message);
+
+ $up_error_message = str_replace('%s',$class::$table,$class::$up_error_message);
+
+ $up_title = str_replace('%s',$class::$table,$class::$up_title);
+
+ $down_title = str_replace('%s',$class::$table,$class::$down_title);
+
+ $exist = $sql->where('version',EQUAL,$date)->exist();
+
+ if ($exist)
{
- $io->success($up_success_message);
- $sql->save(['version'=> $created_at,'migration'=> $migration,'time'=> now()->toDateTimeString()]);
- return 0;
- }
+ $x = equal($env,'prod') ? new $class(production(),$mode,$env) : new $class(development(),$mode,$env);
- $sql->where('version',EQUAL,$date)->destroy();
- $io->error($up_error_message);
- return 1;
- }
+ $io->title("$down_title ($env)");
+
+ $result->push(call_user_func_array([$x, $mode], []));
+
+ if ($result->ok())
+ {
+ $io->success($down_success_message);
+ $sql->where('version',EQUAL,$date)->destroy();
+ return 0;
+ }else
+ {
+ $io->error($down_error_message);
+ return 1;
+ }
+
+ }
+ }
}
- }else
+
+ }
+
+ if (equal($mode,'up'))
{
- foreach ($all->all() as $class => $date)
+ foreach ($all as $class => $date)
{
$migration = static::file($class);
+ $created_at = $class::$created_at;
$down_success_message = str_replace('%s',$class::$table,$class::$down_success_message);
@@ -140,48 +262,38 @@ namespace Eywa\Database\Migration {
if (!$exist)
{
- $x = new $class;
- $io->title($up_title);
- $return->push(call_user_func_array([$x, $mode], []));
+ $x = equal($env,'prod') ? new $class(production(),$mode,$env) : new $class(development(),$mode,$env);
+
+ $io->title("$up_title ($env)");
+
+ $result->push(call_user_func_array([$x, $mode], []));
- if ($return->ok())
+ if ($result->ok())
{
$io->success($up_success_message);
- $sql->save(['version'=> $date,'migration'=> $migration,'time'=> now()->toDateTimeString()]);
+ $sql->save(['version'=> $created_at,'migration'=> $migration,'time'=> now()->toDateTimeString()]);
return 0;
+ }else
+ {
+
+ $sql->where('version',EQUAL,$date)->destroy();
+ $io->error($up_error_message);
+ return 1;
}
- $io->error($up_error_message);
- return 1;
+
}
}
}
- return 0;
- }
-
- /**
- * @param SymfonyStyle $io
- * @param string $mode
- * @return int
- * @throws Kedavra
- */
- private static function rollback(SymfonyStyle $io,string $mode = 'down'): int
- {
-
- $return = collect();
- $all = collect(static::list($mode));
- $sql = sql('migrations');
-
- if (def($sql->execute()))
+ if (equal($mode,'down'))
{
- foreach ($all->all() as $class => $date)
+ foreach ($all as $class => $date)
{
$migration = static::file($class);
$created_at = $class::$created_at;
-
$down_success_message = str_replace('%s',$class::$table,$class::$down_success_message);
$down_error_message = str_replace('%s',$class::$table,$class::$down_error_message);
@@ -194,56 +306,32 @@ namespace Eywa\Database\Migration {
$down_title = str_replace('%s',$class::$table,$class::$down_title);
- $exist = $sql->where('version',EQUAL,$date)->exist() && $sql->where('migration',EQUAL,$migration)->exist();
-
+ $exist = $sql->where('version',EQUAL,$date)->exist();
if ($exist)
{
- $x = new $class;
+ $x = equal($env,'prod') ? new $class(production(),$mode,$env) : new $class(development(),$mode,$env);
- $io->title($down_title);
+ $io->title("$down_title ($env)");
- $return->push(call_user_func_array([$x, $mode], []));
+ $result->push(call_user_func_array([$x, $mode], []));
- if ($return->ok())
+ if ($result->ok())
{
$io->success($down_success_message);
$sql->where('version',EQUAL,$date)->destroy();
+
return 0;
+ }else
+ {
+ $io->error($down_error_message);
+ return 1;
}
- $io->error($down_error_message);
- return 1;
- }
+ }
}
}
-
- return 0;
- }
-
- /**
- * @return bool
- * @throws Kedavra
- */
- public static function check_migrate(): bool
- {
- $sql = sql('migrations');
- $return = collect();
- foreach (static::list('up') as $class => $date)
- {
- $return->push($sql->where('version',EQUAL,$date)->exist());
- }
- return $return->ok();
- }
-
-
- /**
- * @return bool
- * @throws Kedavra
- */
- public static function check_rollback(): bool
- {
- return not_def(sql('migrations')->execute());
+ return 0;
}
}
diff --git a/eywa/Database/Migration/Migration.php b/eywa/Database/Migration/Migration.php
index a0c7d59..3461060 100644
--- a/eywa/Database/Migration/Migration.php
+++ b/eywa/Database/Migration/Migration.php
@@ -6,6 +6,7 @@ namespace Eywa\Database\Migration {
use Exception;
use Eywa\Collection\Collect;
use Eywa\Database\Connexion\Connect;
+ use Eywa\Database\Query\Sql;
use Eywa\Database\Table\Table;
use Eywa\Exception\Kedavra;
@@ -102,6 +103,18 @@ namespace Eywa\Database\Migration {
private static Collect $foreign;
/**
+ * @var Connect
+ */
+ private static Connect $connect;
+
+ /**
+ * @var string
+ */
+ private static string $mode;
+ private static string $env;
+
+
+ /**
*
* The method to updagrade table
*
@@ -126,17 +139,20 @@ namespace Eywa\Database\Migration {
abstract public function down(): bool;
-
/**
* Migration constructor.
*
- * @throws Exception
- *
+ * @param Connect $connect
+ * @param string $mode
+ * @param string $env
*/
- public function __construct()
+ public function __construct(Connect $connect,string $mode,string $env)
{
static::$columns = collect();
static::$foreign = collect();
+ static::$connect = $connect;
+ static::$env = $env;
+ static::$mode = $mode;
}
/**
@@ -177,6 +193,9 @@ namespace Eywa\Database\Migration {
{
$constraint = " FOREIGN KEY ($column) REFERENCES $table($table_column)";
+ if (def($on,$do))
+ append($constraint," ON $on $do");
+
static::$foreign->push(compact('column', 'constraint'));
return $this;
@@ -197,6 +216,7 @@ namespace Eywa\Database\Migration {
* @return Migration
*
* @throws Kedavra
+ *
*/
private function primary(string $column): Migration
{
@@ -249,14 +269,10 @@ namespace Eywa\Database\Migration {
$constraint = collect($x->get('constraints'))->join(' ');
-
-
$type = $x->get('type');
$size = $x->get('size') ?? 0;
-
-
switch ($type)
{
case 'string':
@@ -280,8 +296,6 @@ namespace Eywa\Database\Migration {
}
- static::$columns->clear();
-
foreach (static::$foreign->all() as $foreign)
{
$x = collect($foreign);
@@ -291,13 +305,15 @@ namespace Eywa\Database\Migration {
append($sql," $constraint, ");
}
- static::$foreign->clear();
-
$sql = trim($sql,', ');
append($sql, ')');
- return $this->connexion()->set($sql)->execute();
+ static::$foreign->clear();
+
+ static::$columns->clear();
+
+ return static::connexion()->set($sql)->execute();
}
@@ -313,16 +329,13 @@ namespace Eywa\Database\Migration {
{
$table = static::$table;
- $result = collect();
+ $sql = '';
foreach (static::$columns->all() as $column)
{
- if ((new Table())->from($table)->has($column['column']))
- return false;
-
$type = $column['type'];
$size = $column['size'];
@@ -354,14 +367,13 @@ namespace Eywa\Database\Migration {
append($x,"($size)");
- $result->push($this->connexion()->set("ALTER TABLE $table ADD COLUMN $x;")->execute());
+ append($sql,$x);
}
- static::$columns->clear();
+ return static::connexion()->set("ALTER TABLE $table ADD COLUMN $sql;")->execute();
- return $result->ok();
}
/**
@@ -405,7 +417,7 @@ namespace Eywa\Database\Migration {
*/
public function drop(string $table): bool
{
- return (new Table())->from($table)->drop();
+ return equal(static::$env,'dev') ? (new Table(development()))->from($table)->drop() : (new Table(production()))->from($table)->drop() ;
}
/**
@@ -417,7 +429,7 @@ namespace Eywa\Database\Migration {
*/
public function columns()
{
- return sql(static::$table)->columns();
+ return static::$env === 'dev' ? (new Sql(development(),static::$table))->columns() : (new Sql(production(),static::$table))->columns();
}
/**
@@ -491,21 +503,18 @@ namespace Eywa\Database\Migration {
* @return Connect
*
* @throws Kedavra
- *
*/
private function connexion(): Connect
{
- $prod = new Connect(env('DB_DRIVER','mysql'),env('DB_NAME','eywa'),env('DB_USERNAME','eywa'),env('DB_PASSWORD','eywa'),intval(env('DB_PORT',3306)),config('connection','options'),env('DB_HOST','localhost'));
-
- return equal(config('mode','connexion'),'prod') ? $prod : $prod->development();
+ return static::$env == 'dev' ? development() : production();
}
/**
- * @return string
*
- * @throws Kedavra
*
+ * @return string
*
+ * @throws Kedavra
*/
private function driver(): string
{
diff --git a/eywa/Database/Seed/Seeder.php b/eywa/Database/Seed/Seeder.php
index f5409f9..ce002c6 100644
--- a/eywa/Database/Seed/Seeder.php
+++ b/eywa/Database/Seed/Seeder.php
@@ -123,11 +123,11 @@ namespace Eywa\Database\Seed {
*/
public function seed() : bool
{
- static::$connexion = app()->connexion()->development();
+ static::$connexion = development();
static::$faker = ioc('faker');
- static::$table = (new Table());
+ static::$table = (new Table(development()));
$table = static::$from;
diff --git a/eywa/Database/Table/Table.php b/eywa/Database/Table/Table.php
index 3fdc24c..318cf3a 100644
--- a/eywa/Database/Table/Table.php
+++ b/eywa/Database/Table/Table.php
@@ -45,12 +45,12 @@ namespace Eywa\Database\Table {
*
* Table constructor.
*
- * @throws Kedavra
+ * @param Connect $connect
*/
- public function __construct()
+ public function __construct(Connect $connect)
{
- $prod = new Connect(env('DB_DRIVER','mysql'),env('DB_NAME','eywa'),env('DB_USERNAME','eywa'),env('DB_PASSWORD','eywa'),intval(env('DB_PORT',3306)),config('connection','options'),env('DB_HOST','localhost'));
- $this->connexion = equal(config('mode','connexion'),'prod') ? $prod : $prod->development();
+
+ $this->connexion = $connect;
}
diff --git a/eywa/Ioc/Container.php b/eywa/Ioc/Container.php
index 7721a39..5f7ce11 100644
--- a/eywa/Ioc/Container.php
+++ b/eywa/Ioc/Container.php
@@ -10,6 +10,7 @@ namespace Eywa\Ioc {
use DI\DependencyException;
use DI\NotFoundException;
use Eywa\Application\Environment\Env;
+ use Eywa\Database\Connexion\Connect;
use Eywa\Database\Table\Table;
use Eywa\Exception\Kedavra;
use Eywa\Message\Flash\Flash;
@@ -46,7 +47,7 @@ namespace Eywa\Ioc {
$c->set("views.path",base('app','Views')) ;
$c->set("flash",new Flash()) ;
$c->set('faker',faker(config('i18n','locale'))) ;
- $c->set('table',new Table()) ;
+ $c->set('table',new Table($c->get(Connect::class))) ;
self::$ioc = $c;
}
diff --git a/helpers/admin.php b/helpers/admin.php
index 634bb6a..eaf7e35 100644
--- a/helpers/admin.php
+++ b/helpers/admin.php
@@ -178,7 +178,32 @@ if (!function_exists('app'))
return ioc(App::class);
}
}
+if (!function_exists('production'))
+{
+ /**
+ * @return Connect
+ * @throws Kedavra
+ * @throws Exception
+ */
+ function production(): Connect
+ {
+ return new Connect(env('DB_DRIVER','mysql'),env('DB_NAME','eywa'),env('DB_USERNAME','eywa'),env('DB_PASSWORD','eywa'),intval(env('DB_PORT',3306)),env('DB_HOST','localhost'));
+
+ }
+}
+if (!function_exists('development'))
+{
+ /**
+ * @return Connect
+ * @throws Kedavra
+ * @throws Exception
+ */
+ function development(): Connect
+ {
+ return new Connect(env('DEVELOP_DB_DRIVER','mysql'),env('DEVELOP_DB_NAME','ikran'),env('DEVELOP_DB_USERNAME','ikran'),env('DEVELOP_DB_PASSWORD','ikran'),intval(env('DEVELOP_DB_PORT',3306)),env('DEVELOP_DB_HOST','localhost'));
+ }
+}
if (!function_exists('sql'))
{
/**
@@ -194,13 +219,12 @@ if (!function_exists('sql'))
*/
function sql(string $table): Sql
{
- $prod = new Connect(env('DB_DRIVER','mysql'),env('DB_NAME','eywa'),env('DB_USERNAME','eywa'),env('DB_PASSWORD','eywa'),intval(env('DB_PORT',3306)),config('connection','options'),env('DB_HOST','localhost'));
-
- $connexion = equal(config('mode','connexion'),'prod') ? $prod : $prod->development();
+ $connexion = new Connect(env('DB_DRIVER','mysql'),env('DB_NAME','eywa'),env('DB_USERNAME','eywa'),env('DB_PASSWORD','eywa'),intval(env('DB_PORT',3306)),env('DB_HOST','localhost'));
return (new Sql($connexion,$table));
}
}
+
if (!function_exists('ioc'))
{
/**
@@ -271,7 +295,8 @@ if (!function_exists('config'))
return (new Config($file, $key))->value();
}
}
-if (!function_exists('def')) {
+if (!function_exists('def'))
+{
/**
*
* Check if all values are define
@@ -294,7 +319,8 @@ if (!function_exists('def')) {
return true;
}
}
-if (!function_exists('update_file_values')) {
+if (!function_exists('update_file_values'))
+{
/**
*
* Update a value in a file
@@ -665,17 +691,15 @@ if (!function_exists('connect'))
* @param string $user
* @param string $password
* @param int $port
- * @param array $options
* @param string $host
*
* @return Connect
*
* @throws Kedavra
- *
*/
- function connect(string $driver, string $base, string $user = '', string $password = '', int $port = 3306, array $options = [], string $host = 'localhost'): Connect
+ function connect(string $driver, string $base, string $user = '', string $password = '', int $port = 3306, string $host = 'localhost'): Connect
{
- return new Connect($driver, $base, $user, $password, $port, $options, $host);
+ return new Connect($driver, $base, $user, $password, $port, $host);
}
}
if (!function_exists('superior'))
diff --git a/shaolin b/shaolin
index 27a247f..b00aefe 100644..100755
--- a/shaolin
+++ b/shaolin
@@ -1,6 +1,9 @@
#!/usr/bin/env php
<?php
-require 'vendor/autoload.php';
-exit((new \Eywa\Console\Console())->run()); \ No newline at end of file
+use Eywa\Console\Console;
+
+require __DIR__.'/vendor/autoload.php';
+
+exit((new Console('EYWA','11'))->run()); \ No newline at end of file
diff --git a/tests/App/ApplicationTest.php b/tests/App/ApplicationTest.php
index c5f17bf..6f92131 100644
--- a/tests/App/ApplicationTest.php
+++ b/tests/App/ApplicationTest.php
@@ -28,6 +28,8 @@ class ApplicationTest extends Unit
public function test_ioc()
{
$this->assertInstanceOf(Connexion::class,app()->ioc(Connect::class));
+ $this->assertInstanceOf(Connexion::class,development());
+ $this->assertInstanceOf(Connexion::class,production());
$this->assertInstanceOf(Write::class,app()->write('subject','message','micieli@laposte.net','micieli@laposte.net'));
$this->assertInstanceOf(Response::class,app()->response('i am a view')->send());
$this->assertInstanceOf(Response::class,app()->redirect('root',[],'',true)->send());
@@ -66,8 +68,6 @@ class ApplicationTest extends Unit
$this->assertNull(app()->cookie('a',null));
$this->assertNull(app()->server('a',null));
- $this->assertEquals([],app()->config('connection','options'));
- $this->assertEquals([],app()->config('connection','options'));
}
diff --git a/web/index.php b/web/index.php
index c30bafe..74b36ae 100644
--- a/web/index.php
+++ b/web/index.php
@@ -2,4 +2,6 @@
require '../vendor/autoload.php';
+
+
app()->run();