PHP code example of afterflow / recipe

1. Go to this page and download the library: Download afterflow/recipe library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

afterflow / recipe example snippets



$recipe = new \Afterflow\Recipe\Recipe();
$data   = $recipe->with([ 'name' => 'Vlad', 'last_name' => 'Libre' ])
                 ->template(__DIR__ . '/templates/user.blade.php')
                 ->render();





namespace Afterflow\Recipe\Recipes;

use Afterflow\Recipe\Recipe;

class ClassRecipe extends Recipe
{

    protected $template = __DIR__ . '/../../templates/class.blade.php';

    protected $props = [
        'name'       => [
            'rules' => '> 'string',
        ],
        'imports'    => [
            'default' => [],
            'rules'   => 'array',
        ],
        'implements' => [
            'default' => [],
            'rules'   => 'array',
        ],
        'traits'     => [
            'default' => [],
            'rules'   => 'array',
        ],
    ];

}


$data = ( new ClassRecipe() )->with([
    'namespace' => 'App',
    'name'      => 'User',
    'extends'   => 'Authenticatable',

    'imports' => [
        'Illuminate\Foundation\Auth\User as Authenticatable',
        'Illuminate\Notifications\Notifiable',
        'Laravel\Passport\HasApiTokens',
    ],

    'traits'     => [
        'HasApiTokens',
        'Notifiable',
    ],
    'implements' => [ 'SomeInterface', 'OtherInterface' ],
])->render();



$data = ( new ClassRecipe() )->with([
    'namespace' => 'App',
    'name'      => 'User',
    'extends'   => 'Authenticatable',

    'imports' => [
        'Illuminate\Foundation\Auth\User as Authenticatable',
        'Illuminate\Notifications\Notifiable',
        'Laravel\Passport\HasApiTokens',
    ],

    'traits'     => [
        'HasApiTokens',
        'Notifiable',
    ],
    'implements' => [ 'SomeInterface', 'OtherInterface' ],
])->build();



// Full syntax
$recipe = (new ClassRecipe())->with($data)->render();

// Pass data into constructor:
$recipe = (new ClassRecipe($data))->render();

// Less braces:
$recipe = ClassRecipe::make($data);

// If your recipe defines a template or a custom render() function:
$string = ClassRecipe::quickRender($data);

// Compile data only:
$data = ClassRecipe::quickBuild($data);





namespace Afterflow\Recipe\Recipes;

use Afterflow\Recipe\Recipe;

class FunctionRecipe extends Recipe
{

    protected $template = __DIR__ . '/../../templates/function.blade.php';

    protected $props = [
        'name'       => [
            'rules' => '




namespace Afterflow\Recipe\Recipes;

use Afterflow\Recipe\Recipe;

class ClassVarRecipe extends Recipe
{

    protected $props = [
        'name'       => [
            'rules' => 'string',
        ],
        'static'     => [
            'default' => false,
            'rules'   => 'boolean',
        ],
        'const'      => [
            'default' => false,
            'rules'   => 'boolean',
        ],
        'docBlock'   => [
            'default' => '',
            'rules'   => 'string',
        ],
    ];

    public function render()
    {

        $string = '';

        if ($v = $this->data('docBlock')) {
            $string .= $v . PHP_EOL;
        }

        if ($v = $this->data('visibility')) {
            $string .= $v . ' ';
        }

        if ($this->data('static')) {
            $string .= 'static ';
        }

        if ($this->data('const')) {
            $string .= 'const ';
        }

        $string .= $this->data('name');
        if ($v = $this->data('value')) {
            $string .= ' = ' . $v;
        }

        $string .= ';';

        return $string;
    }
}


    // ...


    public function name($value)
    {
        return $this->input('name', $value);
    }

    public function value($value)
    {
        return $this->input('value', $value);
    }

    public function const()
    {
        return $this->input('const', true);
    }

    // ...



$data = ClassVarRecipe::make()->name( '$name' )
                      ->protected()
                      ->value( '"Vlad"' )
                      ->docBlock( '// First Name' )
                      ->render();



        /**
         * This recipe nests other recipes and shows alternative syntax to pass data through constructor
         */
        $data = ClassRecipe::make()->namespace('App')->name('User')->content(
        /**
         * See ClassVarRecipe to learn how to render things without template
         */
            Recipe::sequence([
                ClassVarRecipe::make()->protected()->name('$name')->docBlock('// First Name')->render(),
                ClassVarRecipe::make()->protected()->name('$lastName')->docBlock('// Last Name')->render(),
                /**
                 * See ClassVarRecipe to learn how to filter data before render
                 */
                ConstructorRecipe::make()->arguments([
                    'string $name',
                    'string $lastName',
                ])->body('$this->name = $name;' . eol() . '$this->lastName = $lastName;')->render(),
                FunctionRecipe::make()->name('getLastName')->return('$this->lastName;')->render(),
                FunctionRecipe::make()->name('getName')->return('$this->name;')->render(),
            ], eol(2))
        )->render();




namespace App;


class User  
{

    // First Name
    protected $name = "Vlad";
    
    // Last Name
    protected $lastName;
    
    function __construct(string $name, string $lastName)
    {
        $this->name = $name;
        $this->lastName = $lastName;
    }
    
    function getLastName()
    {
        return $this->lastName;
    }
    
    function getName()
    {
        return $this->name;
    }
    
}



function q( $what ) {
	return "'" . $what . "'";
}

function qq( $what ) {
	return '"' . $what . '"';
}

function eol( $times = 1 ) {
	$str = '';
	for ( $i = 0; $i < $times; $i ++ ) {
		$str .= PHP_EOL;
	}

	return $str;
}

function arr( $what, $d = [ '[', ']' ] ) {
	return Recipe::array( $what, $d );
}

blade
{{-- templates/user.blade.php --}}

Name: {{$name}}
Last Name: {{ $last_name }}

blade
{{--templates/class.blade.php--}}

{{-- Otherwise this file will be treated as PHP script--}}
{!! '<'.'?php' !!}

@unless(empty( $namespace ))
namespace {{ $namespace }};
@endunless

@unless(empty( $imports ))
    @foreach( $imports as $import)
import {{ $import }};
    @endforeach
@endunless

class {{ $name }} {{ isset($extends) ? 'extends '. $extends : '' }} {{ !empty($implements) ? 'implements '. collect($implements)->implode(', ') : '' }}
{
@unless(empty($traits))
    use {{ collect($traits)->implode(', ') }};
@endunless

@isset($content)
{{--This function indents each line of $content string with 4 spaces--}}
@indent($content,4)
@endisset
}