-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #220 from CasperLaiTW/criteria-generator
Criteria command (make:criteria)
- Loading branch information
Showing
7 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/Prettus/Repository/Generators/Commands/CriteriaCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/Prettus/Repository/Generators/Stubs/criteria/criteria.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters