Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/resources/config/repository.php
  • Loading branch information
ionut-tanasa committed Jun 23, 2016
2 parents e3485c1 + e6fa3ed commit 9a470c6
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 37 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ php artisan vendor:publish
- delete($id)
- orderBy($column, $direction = 'asc');
- with(array $relations);
- has(string $relation);
- whereHas(string $relation, closure $closure);
- hidden(array $fields);
- visible(array $fields);
- scopeQuery(Closure $scope);
Expand Down Expand Up @@ -301,6 +303,12 @@ public function __construct({YOUR_NAMESPACE}Repositories\PostRepository $reposit
}
```

Alternatively, you could use the artisan command to do the binding for you.

```php
php artisan make:bindings Cats
```

### Use methods

```php
Expand Down
4 changes: 2 additions & 2 deletions src/Prettus/Repository/Criteria/RequestCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public function apply($model, RepositoryInterface $repository)
$condition = trim(strtolower($condition));

if (isset($searchData[$field])) {
$value = $condition == "like" ? "%{$searchData[$field]}%" : $searchData[$field];
$value = ($condition == "like" || $condition == "ilike") ? "%{$searchData[$field]}%" : $searchData[$field];
} else {
if (!is_null($search)) {
$value = $condition == "like" ? "%{$search}%" : $search;
$value = ($condition == "like" || $condition == "ilike") ? "%{$search}%" : $search;
}
}

Expand Down
72 changes: 63 additions & 9 deletions src/Prettus/Repository/Eloquent/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ public function scopeQuery(\Closure $scope)
*/
public function lists($column, $key = null)
{
return $this->makeModel()->lists($column, $key);
$this->applyCriteria();

return $this->model->lists($column, $key);
}

/**
Expand Down Expand Up @@ -393,14 +395,7 @@ public function findWhere(array $where, $columns = ['*'])
$this->applyCriteria();
$this->applyScope();

foreach ($where as $field => $value) {
if (is_array($value)) {
list($field, $condition, $val) = $value;
$this->model = $this->model->where($field, $condition, $val);
} else {
$this->model = $this->model->where($field, '=', $value);
}
}
$this->applyConditions($where);

$model = $this->model->get($columns);
$this->resetModel();
Expand Down Expand Up @@ -571,6 +566,32 @@ public function delete($id)
return $deleted;
}

/**
* Delete multiple entities by given criteria.
*
* @param array $where
*
* @return int
*/
public function deleteWhere(array $where)
{
$this->applyScope();

$temporarySkipPresenter = $this->skipPresenter;
$this->skipPresenter(true);

$this->applyConditions($where);

$deleted = $this->model->delete();

event(new RepositoryEntityDeleted($this, $this->model));

$this->skipPresenter($temporarySkipPresenter);
$this->resetModel();

return $deleted;
}

/**
* Check if entity has relation
*
Expand Down Expand Up @@ -598,6 +619,21 @@ public function with($relations)

return $this;
}

/**
* Load relation with closure
*
* @param string $relation
* @param closure $closure
*
* @return $this
*/
function whereHas($relation, $closure)
{
$this->model = $this->model->whereHas($relation, $closure);

return $this;
}

/**
* Set hidden fields
Expand Down Expand Up @@ -783,6 +819,24 @@ protected function applyCriteria()
return $this;
}

/**
* Applies the given where conditions to the model.
*
* @param array $where
* @return void
*/
protected function applyConditions(array $where)
{
foreach ($where as $field => $value) {
if (is_array($value)) {
list($field, $condition, $val) = $value;
$this->model = $this->model->where($field, $condition, $val);
} else {
$this->model = $this->model->where($field, '=', $value);
}
}
}

/**
* Skip Presenter Wrapper
*
Expand Down
18 changes: 18 additions & 0 deletions src/Prettus/Repository/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function getReplacements()
'singular' => $this->getSingularName(),
'validator' => $this->getValidator(),
'repository' => $this->getRepository(),
'manager' => $this->getManager(),
'appname' => $this->getAppNamespace(),
]);
}
Expand All @@ -105,7 +106,24 @@ public function getSingularName()
return str_singular(lcfirst(ucwords($this->getClass())));
}

/**
* Gets manager full class name
*
* @return string
*/
public function getManager()
{
$managerGenerator = new ManagerGenerator([
'name' => $this->name,
]);

$manager = $managerGenerator->getRootNamespace() . '\\' . $managerGenerator->getName();

return 'use ' . str_replace([
"\\",
'/'
], '\\', $manager) . 'Manager;';
}
/**
* Gets validator full class name
*
Expand Down
8 changes: 7 additions & 1 deletion src/Prettus/Repository/Generators/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ public function setFilesystem(Filesystem $filesystem)
*/
public function getStub()
{
return (new Stub(__DIR__ . '/Stubs/' . $this->stub . '.stub', $this->getReplacements()))->render();
$path = config('repository.generator.stubsOverridePath', __DIR__);

if(!file_exists($path . '/Stubs/' . $this->stub . '.stub')){
$path = __DIR__;
}

return (new Stub($path . '/Stubs/' . $this->stub . '.stub', $this->getReplacements()))->render();
}


Expand Down
11 changes: 8 additions & 3 deletions src/Prettus/Repository/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,16 @@ public function getStub()
];
break;
}
$path = config('repository.generator.stubsOverridePath', __DIR__);

if (!file_exists(__DIR__ . "/Stubs/migration/{$file}.stub")) {
throw new FileNotFoundException(__DIR__ . "/Stubs/migration/{$file}.stub");
if (!file_exists($path . "/Stubs/migration/{$file}.stub")) {
$path = __DIR__;
}

return Stub::create(__DIR__ . "/Stubs/migration/{$file}.stub", $replacements);
if (!file_exists($path . "/Stubs/migration/{$file}.stub")) {
throw new FileNotFoundException($path . "/Stubs/migration/{$file}.stub");
}

return Stub::create($path . "/Stubs/migration/{$file}.stub", $replacements);
}
}
30 changes: 11 additions & 19 deletions src/Prettus/Repository/Generators/Stubs/controller/controller.stub
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ use Prettus\Validator\Contracts\ValidatorInterface;
use Prettus\Validator\Exceptions\ValidatorException;
use $APPNAME$Http\Requests\$CLASS$CreateRequest;
use $APPNAME$Http\Requests\$CLASS$UpdateRequest;
use $APPNAME$Http\Requests\$CLASS$UpdateRequest;
$REPOSITORY$
$VALIDATOR$
$MANAGER$


class $CONTROLLER$Controller extends Controller
Expand All @@ -25,12 +27,16 @@ class $CONTROLLER$Controller extends Controller
* @var $CLASS$Validator
*/
protected $validator;
/**
* @var $CLASS$Manager
*/
protected $manager;


public function __construct($CLASS$Repository $repository, $CLASS$Validator $validator)
public function __construct($CLASS$Repository $repository, $CLASS$Validator $validatorm, $CLASS$Manager)
{
$this->repository = $repository;
$this->validator = $validator;
$this->manager = $manager;
}


Expand All @@ -41,7 +47,6 @@ class $CONTROLLER$Controller extends Controller
*/
public function index()
{

$this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
$$PLURAL$ = $this->repository->all();

Expand All @@ -55,19 +60,6 @@ class $CONTROLLER$Controller extends Controller
return view('$PLURAL$.index', compact('$PLURAL$'));
}


/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{

return view('$PLURAL$.create');
}


/**
* Store a newly created resource in storage.
*
Expand All @@ -82,7 +74,7 @@ class $CONTROLLER$Controller extends Controller

$this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);

$$SINGULAR$ = $this->repository->create($request->all());
$$SINGULAR$ = $this->manager->create($request->all());

$response = [
'message' => '$CLASS$ created.',
Expand Down Expand Up @@ -161,7 +153,7 @@ class $CONTROLLER$Controller extends Controller

$this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);

$$SINGULAR$ = $this->repository->update($request->all(), $id);
$$SINGULAR$ = $this->manager->update($id, $request->all());

$response = [
'message' => '$CLASS$ updated.',
Expand Down Expand Up @@ -198,7 +190,7 @@ class $CONTROLLER$Controller extends Controller
*/
public function destroy($id)
{
$deleted = $this->repository->delete($id);
$deleted = $this->manager->delete($id);

if (request()->wantsJson()) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ $NAMESPACE$
use \Prettus\Validator\Contracts\ValidatorInterface;
use \Prettus\Validator\LaravelValidator;

class $CLASS$Validator extends LaravelValidator {
class $CLASS$Validator extends LaravelValidator
{

protected $rules = [
ValidatorInterface::RULE_CREATE => $RULES$,
ValidatorInterface::RULE_UPDATE => $RULES$,
];

}
}
1 change: 1 addition & 0 deletions src/resources/config/repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
'controllers' => 'Http/Controllers',
'provider' => 'RepositoryServiceProvider',
'criteria' => 'Criteria',
'stubsOverridePath' => app_path()
]
]
];

0 comments on commit 9a470c6

Please sign in to comment.