Download the PHP package afterflow/recipe without Composer
On this page you can find all versions of the php package afterflow/recipe. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package recipe
Afterflow Recipe
Recipe is a generator framework built with Laravel components that allows you to generate anything based on the provided data and templates.
The primary goal of this library is to provide tooling required to create your generators.
Requirements
Although it's relying on Blade templating system from Laravel Framework, this library does not require Laravel, it only pulls some of it's components.
This means you can safely include it in your own framework-agnostic composer package.
Installation
Basic Usage
Given we have a simple Blade template:
Let's compile a string with Recipe:
Returns:
So basically we just compiled a Blade template with the given data. But that's not actually the point. Let's now see some advanced usage.
Custom Recipe classes
Let's build a simple custom recipe we can reuse or even nest on other recipes. It will create a class from a stub and return the source code.
Template:
Recipe:
Usage:
Few new things happen here since we are now using our own ClassRecipe
class that extends Recipe
.
This allows us to define template inside the class and have a shorter usage syntax.
Here you can notice that we're defining a new $props
variable which is somewhat similar to what VueJs uses in it's components.
First, we added some validation telling Recipe that name
data property is mandatory in this recipe.
You can define validation rules just like you normally would in your Laravel application - that's the same thing.
Second, we're setting default value for imports. Those defaults will be applied if the user does not provide anything as the input.
Building data without rendering
Sometimes it may be useful to only build data off of the user input and defined props without template at all:
If you call build()
instead of render()
you'll get back the normalized data after applying props on original input.
You can use this data in other recipes to create compound generators.
Alternative syntax
A shorter syntax might be useful when building complex nested recipes.
Prepare template data before rendering
Sometimes it's useful to transform the data before sending it to Blade compiler. If your recipe has dataForTemplate()
method,
it's return value will be used as the data for the template.
Custom rendering
By overriding the render()
method in your recipe you can create recipes without template or define any other custom rendering logic.
Just make sure to call $this->build()
inside to apply props to the input.
Implement Fluent API
To make process a little more fun and simple, you can sprinkle some fluency onto your recipe:
Then you can use this recipe like that:
Or use MagicSetters
trait on your recipe to achieve the same functionality (this will not handle IDE highlighting though);
Nested recipes
Now let's see how powerful this can be:
Since Recipe implements __toString
, you can omit render()
calls here.
This will produce:
Now you can extend or nest the ClassRecipe
to make a Laravel Model recipe and create a console command to generate a model or do any other crazy stuff.
Built in recipes you can use (work in progress):
- ClassRecipe
- ClassVarRecipe
- FunctionRecipe
- MethodCallRecipe
- ConstructorRecipe
- Laravel/Models/ModelRecipe
- Laravel/Models/Relations/RelationRecipe
Directives
Helpers
All helpers are namespaced:
All versions of recipe with dependencies
illuminate/events Version 5.*|6.*|7.*
illuminate/filesystem Version 5.*|6.*|7.*