Skip to content

Commit

Permalink
Merge pull request #220 from CasperLaiTW/criteria-generator
Browse files Browse the repository at this point in the history
Criteria command (make:criteria)
  • Loading branch information
ionut-tanasa authored Jun 23, 2016
2 parents 8ed5d45 + f583ac7 commit e3485c1
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ You must first configure the storage location of the repository files. By defaul
'validators' => 'Validators',
'controllers' => 'Http/Controllers',
'provider' => 'RepositoryServiceProvider',
'criteria' => 'Criteria',
]
]
```
Expand Down Expand Up @@ -231,6 +232,7 @@ Additionally, you may wish to customize where your generated classes end up bein
'validators' => 'Validators',
'controllers' => 'Http/Controllers',
'provider' => 'RepositoryServiceProvider',
'criteria' => 'Criteria',
]
]
```
Expand Down Expand Up @@ -415,6 +417,12 @@ $this->repository->delete($id)

### Create a Criteria

#### Using the command

```terminal
php artisan make:criteria My
```

Criteria are a way to change the repository of the query by applying specific conditions according to your needs. You can add multiple Criteria in your repository.

```php
Expand Down
87 changes: 87 additions & 0 deletions src/Prettus/Repository/Generators/Commands/CriteriaCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Prettus\Repository\Generators\Commands;

use Illuminate\Console\Command;
use Prettus\Repository\Generators\CriteriaGenerator;
use Prettus\Repository\Generators\FileAlreadyExistsException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class CriteriaCommand extends Command
{
/**
* The name of command.
*
* @var string
*/
protected $name = 'make:criteria';

/**
* The description of command.
*
* @var string
*/
protected $description = 'Create a new criteria.';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Criteria';
/**
* Execute the command.
*
* @return void
*/
public function fire()
{
try {
(new CriteriaGenerator([
'name' => $this->argument('name'),
'force' => $this->option('force'),
]))->run();

$this->info("Criteria created successfully.");
} catch (FileAlreadyExistsException $ex) {
$this->error($this->type . ' already exists!');
return false;
}
}

/**
* The array of command arguments.
*
* @return array
*/
public function getArguments()
{
return [
[
'name',
InputArgument::REQUIRED,
'The name of class being generated.',
null
],
];
}

/**
* The array of command options.
*
* @return array
*/
public function getOptions()
{
return [
[
'force',
'f',
InputOption::VALUE_NONE,
'Force the creation if file already exists.',
null
],
];
}
}
56 changes: 56 additions & 0 deletions src/Prettus/Repository/Generators/CriteriaGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Prettus\Repository\Generators;

/**
* Class CriteriaGenerator
* @package Prettus\Repository\Generators
*/
class CriteriaGenerator extends Generator
{
/**
* Get stub name.
*
* @var string
*/
protected $stub = 'criteria/criteria';

/**
* Get root namespace.
*
* @return string
*/
public function getRootNamespace()
{
return parent::getRootNamespace() . parent::getConfigGeneratorClassPath($this->getPathConfigNode());
}

/**
* Get generator path config node.
* @return string
*/
public function getPathConfigNode()
{
return 'criteria';
}

/**
* Get destination path for generated file.
*
* @return string
*/
public function getPath()
{
return $this->getBasePath() . '/' . parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) . '/' . $this->getName() . 'Criteria.php';
}

/**
* Get base path of destination file.
*
* @return string
*/
public function getBasePath()
{
return config('repository.generator.basePath', app_path());
}
}
3 changes: 3 additions & 0 deletions src/Prettus/Repository/Generators/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ public function getConfigGeneratorClassPath($class, $directoryPath = false)
case ('provider' === $class):
$path = config('repository.generator.paths.provider', 'RepositoryServiceProvider');
break;
case ('criteria' === $class):
$path = config('repository.generator.paths.criteria', 'Criteria');
break;
default:
$path = '';
}
Expand Down
26 changes: 26 additions & 0 deletions src/Prettus/Repository/Generators/Stubs/criteria/criteria.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

$NAMESPACE$

use Prettus\Repository\Contracts\CriteriaInterface;
use Prettus\Repository\Contracts\RepositoryInterface;

/**
* Class $CLASS$Criteria
* @package $NAMESPACE$
*/
class $CLASS$Criteria implements CriteriaInterface
{
/**
* Apply criteria in query repository
*
* @param $model
* @param RepositoryInterface $repository
*
* @return mixed
*/
public function apply($model, RepositoryInterface $repository)
{
return $model;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function register()
$this->commands('Prettus\Repository\Generators\Commands\ValidatorCommand');
$this->commands('Prettus\Repository\Generators\Commands\ControllerCommand');
$this->commands('Prettus\Repository\Generators\Commands\BindingsCommand');
$this->commands('Prettus\Repository\Generators\Commands\CriteriaCommand');
$this->app->register('Prettus\Repository\Providers\EventServiceProvider');
}

Expand Down
1 change: 1 addition & 0 deletions src/resources/config/repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
'validators' => 'Validators',
'controllers' => 'Http/Controllers',
'provider' => 'RepositoryServiceProvider',
'criteria' => 'Criteria',
]
]
];

0 comments on commit e3485c1

Please sign in to comment.