From 01c5f4955105c5a9122d8875279668e3d0f68a39 Mon Sep 17 00:00:00 2001 From: denmasyarikin Date: Sat, 11 Mar 2017 22:25:28 +0700 Subject: [PATCH] [UPDATE] update and fix some issues --- README.md | 10 ++++- .../OrmManager/Consoles/ModelDetail.php | 14 +++---- .../OrmManager/LumenServiceProvider.php | 40 ------------------ ...ider.php => OrmManagerServiceProvider.php} | 4 +- .../OrmManager/Relations/BelongsToMany.php | 2 +- src/Flipbox/OrmManager/Relations/Relation.php | 42 +++++++++++++++---- 6 files changed, 52 insertions(+), 60 deletions(-) delete mode 100644 src/Flipbox/OrmManager/LumenServiceProvider.php rename src/Flipbox/OrmManager/{LaravelServiceProvider.php => OrmManagerServiceProvider.php} (88%) diff --git a/README.md b/README.md index 206ea1b..442489a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,10 @@ # Laravel ORM Manager + +[![Total Downloads](https://poser.pugx.org/flipbox/orm-manager/d/total.svg)](https://packagist.org/packages/flipbox/orm-manager) +[![Latest Stable Version](https://poser.pugx.org/flipbox/orm-manager/v/stable.svg)](https://packagist.org/packages/flipbox/orm-manager) +[![Latest Unstable Version](https://poser.pugx.org/flipbox/orm-manager/v/unstable.svg)](https://packagist.org/packages/flipbox/orm-manager) +[![License](https://poser.pugx.org/flipbox/orm-manager/license.svg)](https://packagist.org/packages/flipbox/orm-manager) + This package is manager for laravel or lumen ORM (object relational mapping) Model. You can generate relation method and control Model development in your project. **Relation method** is method in Model class that reference to another Model for get data in related Model. For example you have a model **User** and **Phone** with relation one to one. Both model will be called connected if there is relation method in both class. In class User.php there should be ```php @@ -35,11 +41,11 @@ composer require flipbox/orm-manager ``` Add service provider for Laravel in the file `config/app.php` ``` -Flipbox\OrmManager\LaravelServiceProvider::class, +Flipbox\OrmManager\OrmManagerServiceProvider::class, ``` Add service provider for Lumen in the file `bootstrap/app.php` ``` -$app->register(Flipbox\OrmManager\LumenServiceProvider::class); +$app->register(Flipbox\OrmManager\OrmManagerServiceProvider::class); ``` ## Features See `php artisan` in console, if you install this package correctly you will see list of features with prefix `orm:` diff --git a/src/Flipbox/OrmManager/Consoles/ModelDetail.php b/src/Flipbox/OrmManager/Consoles/ModelDetail.php index 211c106..9685f60 100644 --- a/src/Flipbox/OrmManager/Consoles/ModelDetail.php +++ b/src/Flipbox/OrmManager/Consoles/ModelDetail.php @@ -97,7 +97,7 @@ protected function showDetail(Model $model) $this->showDatabaseFields($model); $this->showRelatoins($model); - $this->showProperty($model); + $this->showModifier($model); } /** @@ -149,18 +149,18 @@ protected function showRelatoins(Model $model) } /** - * show table property + * show model modifier * * @param Model $model * @return void */ - protected function showProperty(Model $model) + protected function showModifier(Model $model) { $name = $this->manager->getClassName($model); - $this->title("Property of Model {$name}: "); + $this->title("Modifier of Model {$name}: "); - $properties = [ + $modifiers = [ 'mutators' => $this->manager->getMutators($model), 'accessors' => $this->manager->getAccessors($model), 'scopes' => $this->manager->getScopes($model), @@ -168,10 +168,10 @@ protected function showProperty(Model $model) $rows = []; - foreach (max($properties) as $property) { + foreach (max($modifiers) as $modifier) { foreach (['mutator', 'accessor', 'scope'] as $type) { $$type = ''; - $types = &$properties[Str::plural($type)]; + $types = &$modifiers[Str::plural($type)]; if ($types->count() > 0) { $$type = $this->paintString($types->first()->getName(), 'brown'); diff --git a/src/Flipbox/OrmManager/LumenServiceProvider.php b/src/Flipbox/OrmManager/LumenServiceProvider.php deleted file mode 100644 index d1d5914..0000000 --- a/src/Flipbox/OrmManager/LumenServiceProvider.php +++ /dev/null @@ -1,40 +0,0 @@ -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, - ]); - } -} diff --git a/src/Flipbox/OrmManager/LaravelServiceProvider.php b/src/Flipbox/OrmManager/OrmManagerServiceProvider.php similarity index 88% rename from src/Flipbox/OrmManager/LaravelServiceProvider.php rename to src/Flipbox/OrmManager/OrmManagerServiceProvider.php index cffc800..81edfbe 100644 --- a/src/Flipbox/OrmManager/LaravelServiceProvider.php +++ b/src/Flipbox/OrmManager/OrmManagerServiceProvider.php @@ -4,7 +4,7 @@ use Illuminate\Support\ServiceProvider; -class LaravelServiceProvider extends ServiceProvider +class OrmManagerServiceProvider extends ServiceProvider { /** * Perform post-registration booting of services. @@ -12,7 +12,7 @@ class LaravelServiceProvider extends ServiceProvider public function boot() { $this->publishes([ - __DIR__.'/Config/orm.php' => config_path('orm.php'), + __DIR__.'/Config/orm.php' => base_path('config/orm.php'), ], 'config'); } diff --git a/src/Flipbox/OrmManager/Relations/BelongsToMany.php b/src/Flipbox/OrmManager/Relations/BelongsToMany.php index e5a3272..e40c7c9 100644 --- a/src/Flipbox/OrmManager/Relations/BelongsToMany.php +++ b/src/Flipbox/OrmManager/Relations/BelongsToMany.php @@ -69,7 +69,7 @@ protected function setConnectedRelationOptions() if (! $this->db->isTableExists($pivotTable)) { $question = "Can't find table {$this->text['pivot_table']} in the database as {$this->text['pivot_text']}, choice one!"; $pivotTable = $this->options['pivot_table'] = $this->command->choice( - $question, $this->getTables()); + $question, $this->getTables()->keys()->toArray()); $this->text['pivot_table'] = "[".$this->command->paintString($pivotTable, 'green')."]"; } diff --git a/src/Flipbox/OrmManager/Relations/Relation.php b/src/Flipbox/OrmManager/Relations/Relation.php index 65e0227..ab7354c 100644 --- a/src/Flipbox/OrmManager/Relations/Relation.php +++ b/src/Flipbox/OrmManager/Relations/Relation.php @@ -70,11 +70,11 @@ abstract class Relation protected $requiredOptions = []; /** - * new line + * prefix new line * * @var string */ - protected $newline = "\n"; + protected $prefixNewline = PHP_EOL; /** * reverse operation @@ -343,7 +343,7 @@ protected function clearDefaultModelContent($modelCode) preg_match("[\s+\/\/]", $modelCode, $matches); if (count($matches) > 0) { - $this->newline = ""; + $this->prefixNewline = ""; $modelCode = preg_replace("[\s+\/\/]", '', $modelCode); } @@ -360,10 +360,36 @@ protected function clearDefaultModelContent($modelCode) */ protected function writeMethodToFile($filePath, $modelCode, $methodCode) { - file_put_contents ( - $filePath, - str_replace("\n}\n", $this->newline."\n".$methodCode."\n}\n", $modelCode) - ); + $modelCode = $this->normalizeLineEndings($modelCode); + $fileCode = $this->appendMethodToClass($modelCode, $methodCode); + + file_put_contents($filePath, $fileCode); + } + + /** + * Normalizes all line endings in this string + * + * @param string $string + * @return string + */ + protected function normalizeLineEndings($string) + { + return preg_replace('/\R/u', PHP_EOL, $string); + } + + /** + * append method to class + * + * @param string $modelCode + * @param string $modelCode + * @return string + */ + protected function appendMethodToClass($modelCode, $methodCode) + { + $pattern = "/(\})[^\}]*$/"; + $methodCode = $this->prefixNewline.$methodCode.PHP_EOL."}".PHP_EOL; + + return preg_replace($pattern, $methodCode, $modelCode); } /** @@ -382,7 +408,7 @@ protected function setNotConnectedRelationOptions() }); $rules[] = 'confirm that you will create the database schema as above!'; - $confirm = implode("\n ", $rules); + $confirm = implode(PHP_EOL.' ', $rules); if (! $this->command->confirm($confirm, true)) { $this->command->warn('You are trying to use custome options to connect models!');