Custom generations
Simple custom generation
Laravel Idea allows to create a custom code generations. They can be created and configured in the Settings(or Preferences) window: Languages & Frameworks | Laravel Idea | Custom Generations. Simple custom generation:
New "Create Service" generation will appear in the Laravel Idea code generations menu
app/Services/FooService.php
file will be created with this content:
<?php
namespace App\Services;
class FooService
{
}
Complex custom generation
Let's create a new file template for more complex generation. File | Settings (or Preferences) | Editor | File and Code Templates
Choose "Default" scheme to store the template inside your IDE files or "Project" scheme to store template in your projects .idea
folder (it can be useful to share templates with team).
Laravel Idea Repository template code:
#parse("Laravel Ideal Header.php")
use $MODEL_CLASS;
#parse("Laravel Ideal Class Name.php")
{
/**
* Returns $MODEL_CLASS_SINGULAR by id
* @param $id
* @return ${MODEL_CLASS_NAME}
*/
public function get($id) {
return $MODEL_CLASS_NAME::findOrFail($id);
}
/**
* Returns all $MODEL_CLASS_PLURAL
* @return ${MODEL_CLASS_NAME}[]
*/
public function all() {
return $MODEL_CLASS_NAME::all();
}
#if($SAVE)
/**
* Saves $MODEL_CLASS_SINGULAR
* @param ${MODEL_CLASS_NAME} ${MODEL_CLASS_VARIABLE}
*/
public function save(${MODEL_CLASS_VARIABLE}) {
${MODEL_CLASS_VARIABLE}->save();
}
#end
}
I'll explain a bit later about all these ${MODEL_CLASS_NAME}
and other variables. Create a new custom generation:
The new Create Repository generation will ask 2 parameters:
And create this code:
<?php
namespace App\Repositories;
use App\Models\ProjectCategory;
class ProjectCategoryRepository extends Repository
{
/**
* Returns project category by id
* @param $id
* @return ProjectCategory
*/
public function get($id)
{
return ProjectCategory::findOrFail($id);
}
/**
* Returns all project categories
* @return ProjectCategory[]
*/
public function all()
{
return ProjectCategory::all();
}
/**
* Saves project category
* @param ProjectCategory $projectCategory
*/
public function save($projectCategory)
{
$projectCategory->save();
}
}
Code templates
Templates are processed by Apache Velocity. Documentation.
Custom generation parameters have "Template variable name" option and it's value can be used in the template. If template variable name ends with "_CLASS" Laravel Idea creates a lot of other variables. For example if MODEL_CLASS variable has App\Models\ProjectCategory value these variables with values will be available in the template:
Variable | Value |
---|---|
MODEL_CLASS | App\Models\ProjectCategory |
MODEL_CLASS_NAME | ProjectCategory |
MODEL_CLASS_NAME_PLURAL | ProjectCategories |
MODEL_CLASS_NAMESPACE | App\Models |
MODEL_CLASS_FQN | \App\Models\ProjectCategory |
MODEL_CLASS_VARIABLE | $projectCategory |
MODEL_CLASS_VARIABLE_PLURAL | $projectCategories |
MODEL_CLASS_VARIABLE_SNAKE | $project_category |
MODEL_CLASS_VARIABLE_SNAKE_PLURAL | $project_categories |
MODEL_CLASS_FIELD | projectCategory |
MODEL_CLASS_FIELD_PLURAL | projectCategories |
MODEL_CLASS_FIELD_SNAKE | project_category |
MODEL_CLASS_FIELD_SNAKE_PLURAL | project_categories |
MODEL_CLASS_SINGULAR | project category |
MODEL_CLASS_SINGULAR_UC | Project category |
MODEL_CLASS_PLURAL | project categories |
MODEL_CLASS_PLURAL_UC | Project categories |
MODEL_CLASS_SLUG | project-category |
MODEL_CLASS_SLUG_PLURAL | project-categories |