Skip to content

Commit

Permalink
[UPDATE] update and fix some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
denmasyarikin committed Mar 11, 2017
1 parent c3b170d commit 01c5f49
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 60 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:`
Expand Down
14 changes: 7 additions & 7 deletions src/Flipbox/OrmManager/Consoles/ModelDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function showDetail(Model $model)

$this->showDatabaseFields($model);
$this->showRelatoins($model);
$this->showProperty($model);
$this->showModifier($model);
}

/**
Expand Down Expand Up @@ -149,29 +149,29 @@ 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),
];

$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');
Expand Down
40 changes: 0 additions & 40 deletions src/Flipbox/OrmManager/LumenServiceProvider.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

use Illuminate\Support\ServiceProvider;

class LaravelServiceProvider extends ServiceProvider
class OrmManagerServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*/
public function boot()
{
$this->publishes([
__DIR__.'/Config/orm.php' => config_path('orm.php'),
__DIR__.'/Config/orm.php' => base_path('config/orm.php'),
], 'config');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Flipbox/OrmManager/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')."]";
}
Expand Down
42 changes: 34 additions & 8 deletions src/Flipbox/OrmManager/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

/**
Expand All @@ -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!');
Expand Down

0 comments on commit 01c5f49

Please sign in to comment.