Skip to content

Commit

Permalink
[UPDATE] Support Lumen
Browse files Browse the repository at this point in the history
  • Loading branch information
dedensaka committed Mar 4, 2017
1 parent c50722a commit 2846b28
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 85 deletions.
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
"license": "MIT",
"authors": [
{
"name": "denmasyarikin",
"name": "Denma Syarikin",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.6.4",
"laravel/framework": "^5.2",
"doctrine/dbal": "v2.5.5"
"illuminate/config": "^5.2",
"illuminate/console": "^5.2",
"illuminate/database": "^5.2",
"illuminate/support": "^5.2",
"doctrine/dbal": "^2.5"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 2 additions & 3 deletions src/Flipbox/OrmManager/Config/orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
|--------------------------------------------------------------------------
| Model Base Path
|--------------------------------------------------------------------------
| base path of models should be path of folder of model
| by default is app/
| base path of models
*/

'basepath' => app_path(),
'basepath' => 'app',

/*
|--------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion src/Flipbox/OrmManager/Consoles/ModelAutoConnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use Illuminate\Support\Str;
use Illuminate\Config\Repository;
use Flipbox\OrmManager\ModelManager;
use Illuminate\Database\Eloquent\Model;
use Flipbox\OrmManager\DatabaseConnection;
Expand Down
1 change: 0 additions & 1 deletion src/Flipbox/OrmManager/Consoles/ModelBothConnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Config\Repository;
use Flipbox\OrmManager\ModelManager;
use Illuminate\Database\Eloquent\Model;
use Flipbox\OrmManager\Exceptions\ModelNotFound;
Expand Down
39 changes: 20 additions & 19 deletions src/Flipbox/OrmManager/Consoles/ModelConnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Exception;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Illuminate\Config\Repository;
use Flipbox\OrmManager\ModelManager;
use Illuminate\Database\Eloquent\Model;
use Flipbox\OrmManager\DatabaseConnection;
Expand All @@ -20,20 +19,20 @@ class ModelConnect extends Command
FontColor::paintString insteadof LocalComand;
}

/**
* database
*
* @var DatabaseConnection
*/
protected $db;

/**
* model manager
*
* @var ModelManager
*/
protected $manager;

/**
* database
*
* @var ModelManager
*/
protected $db;

/**
* The console command name.
*
Expand All @@ -50,17 +49,19 @@ class ModelConnect extends Command
protected $description = 'Generate connections method of class model';

/**
* Create a new ModelConnect instance.
*
* @param Repository $config
* @return void
*/
public function __construct(Repository $config)
{
parent::__construct();
$this->db = new DatabaseConnection;
$this->manager = new ModelManager($config['orm'], $this->db);
}
* Create a new queue listen command.
*
* @param DatabaseConnection $db
* @param ModelManager $manager
* @return void
*/
public function __construct(DatabaseConnection $db, ModelManager $manager)
{
parent::__construct();

$this->db = $db;
$this->manager = $manager;
}

/**
* Execute the console command.
Expand Down
20 changes: 10 additions & 10 deletions src/Flipbox/OrmManager/Consoles/ModelDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Flipbox\OrmManager\Consoles;

use ReflectionClass;
use Illuminate\Console\Command;
use Illuminate\Config\Repository;
use Flipbox\OrmManager\ModelManager;
use Illuminate\Database\Eloquent\Model;
use Flipbox\OrmManager\DatabaseConnection;
Expand All @@ -17,18 +15,18 @@ class ModelDetail extends Command
}

/**
* model manager
* database
*
* @var ModelManager
* @var DatabaseConnection
*/
protected $manager;
protected $db;

/**
* database
* model manager
*
* @var ModelManager
*/
protected $db;
protected $manager;

/**
* The console command name.
Expand All @@ -47,14 +45,16 @@ class ModelDetail extends Command
/**
* Create a new queue listen command.
*
* @param DatabaseConnection $db
* @param ModelManager $manager
* @return void
*/
public function __construct(Repository $config)
public function __construct(DatabaseConnection $db, ModelManager $manager)
{
parent::__construct();

$this->db = new DatabaseConnection;
$this->manager = new ModelManager($config->get('orm'), $this->db);
$this->db = $db;
$this->manager = $manager;
}

/**
Expand Down
30 changes: 16 additions & 14 deletions src/Flipbox/OrmManager/Consoles/ModelList.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

use Exception;
use Illuminate\Console\Command;
use Illuminate\Config\Repository;
use Flipbox\OrmManager\ModelManager;
use Flipbox\OrmManager\DatabaseConnection;
use Flipbox\OrmManager\Consoles\Command as LocalComand;

class ModelList extends Command
{
use LocalComand, FontColor {
FontColor::paintstring insteadof LocalComand;
FontColor::paintString insteadof LocalComand;
}

/**
Expand Down Expand Up @@ -46,26 +45,29 @@ class ModelList extends Command
/**
* Create a new queue listen command.
*
* @param DatabaseConnection $db
* @param ModelManager $manager
* @return void
*/
public function __construct(Repository $config)
public function __construct(DatabaseConnection $db, ModelManager $manager)
{
parent::__construct();

$this->db = new DatabaseConnection;
$this->manager = new ModelManager($config->get('orm'), $this->db);
$this->db = $db;
$this->manager = $manager;
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
try {
$models = $this->manager->toArray();
} catch (Exception $e) {
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
try {
$models = $this->manager->toArray();
} catch (Exception $e) {
dd($e);
return $this->error($e->getMessage());
}

Expand Down
21 changes: 17 additions & 4 deletions src/Flipbox/OrmManager/DatabaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

namespace Flipbox\OrmManager;

use DB;
use Exception;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Column;
use Illuminate\Support\Collection;
use Doctrine\DBAL\Driver\PDOException;
use Illuminate\Database\DatabaseManager;

class DatabaseConnection
{
/**
* database manager
*
* @var DatabaseManager
*/
protected $db;

/**
* check connection
*
Expand All @@ -36,8 +44,10 @@ class DatabaseConnection
*
* @return void
*/
public function __construct()
public function __construct(DatabaseManager $db)
{
$this->db = $db;

$this->initDoctrine();
$this->scanDatabase();
}
Expand All @@ -51,11 +61,14 @@ public function __construct()
protected function initDoctrine()
{
try {
$this->doctrine = DB::getDoctrineSchemaManager();
$this->doctrine = $this->db->getDoctrineSchemaManager();

$platform = $this->doctrine->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');

$this->connection = true;
} catch (Exception $e) {
} catch (PDOException $e) {
echo "{$e->getMessage()}\n";
$this->connection = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Flipbox\OrmManager;

use Flipbox\OrmManager\Console;
use Illuminate\Support\ServiceProvider;

class OrmManagerServiceProvider extends ServiceProvider
class LaravelServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
Expand All @@ -24,22 +23,20 @@ public function register()
{
$this->mergeConfigFrom(__DIR__.'/Config/orm.php', 'orm');

$this->registerCommand();
}
$this->app->singleton('orm.database', function ($app) {
return new DatabaseConnection($app['db']);
});

/**
* register commands
*
* @return void
*/
protected function registerCommand()
{
$this->commands([
$this->app->singleton('orm.manager', function ($app) {
return new ModelManager($app['config'], $app['orm.database']);
});

$this->commands([
Consoles\ModelList::class,
Consoles\ModelDetail::class,
Consoles\ModelDetail::class,
Consoles\ModelConnect::class,
Consoles\ModelBothConnect::class,
Consoles\ModelAutoConnect::class,
]);
]);
}
}
40 changes: 40 additions & 0 deletions src/Flipbox/OrmManager/LumenServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Flipbox\OrmManager;

use Illuminate\Support\ServiceProvider;

class LumenServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*/
public function boot()
{
//
}

/**
* Register any application services.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/Config/orm.php', 'orm');

$this->app->singleton('orm.database', function ($app) {
return new DatabaseConnection($app['db']);
});

$this->app->singleton('orm.manager', function ($app) {
return new ModelManager($app['config'], $app['orm.database']);
});

$this->commands([
Consoles\ModelList::class,
Consoles\ModelDetail::class,
Consoles\ModelConnect::class,
Consoles\ModelBothConnect::class,
Consoles\ModelAutoConnect::class,
]);
}
}
Loading

0 comments on commit 2846b28

Please sign in to comment.