PHP code example of kenokokoro / laravel-basetree

1. Go to this page and download the library: Download kenokokoro/laravel-basetree 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/ */

    

kenokokoro / laravel-basetree example snippets


    public function register() 
    {
        $this->app->register(BaseTree\Providers\BaseTreeServiceProvider::class);
    }

        namespace App\Models;
        
        use Illuminate\Database\Eloquent\Model;
        use BaseTree\Models\BaseTreeModel;
        
        class Foo extends Model implements BaseTreeModel 
        {
            protected $fillable = ['name', 'description'];
        }
    

        namespace App\DAL\Foo;

        use BaseTree\Eloquent\RepositoryInterface;

        interface FooRepository extends RepositoryInterface 
        {
        }
    

        namespace App\DAL\Foo;

        use BaseTree\Eloquent\BaseEloquent;
        use App\Models\Foo;

        class EloquentFoo extends BaseEloquent implements FooRepository 
        {
            public function __construct(Foo $model)
            {
                parent::__construct($model);
            }
        }
    

        namespace App\DAL;

        use Illuminate\Support\ServiceProvider;
        use App\DAL\Foo\FooRepository;
        use App\DAL\Foo\EloquentFoo;
        
        class DalServiceProvider extends ServiceProvider 
        {
            public function register() {
                $bindings = [
                    FooRepository::class => EloquentFoo::class,
                    # Every other repository should be registered here
                ];
                
                foreach($bindings as $interface => $implementation) {
                    $this->app->bind($interface, $implementation);
                }
            }
        }
    

        use App\DAL\DalServiceProvider;

        ...
        public function register() {
            $this->app->register(DalServiceProvider::class);
        }
    

        namespace App\BLL;
        
        use BaseTree\BLL\BaseResource
        use App\DAL\FooRepository;
        
        class FooResource extends BaseResource
        {
            public function __construct(FooRepository $repository) 
            {
                parent::__construct($repository);
            }
        }
    

        namespace App\Http\Controllers;
        
        use BaseTree\Controllers\Laravel\JsonController;
        use App\BLL\FooResource;
        
        class FoosController extends JsonController 
        {
            public function __construct(FooResource $resource) 
            {
                parent::__construct($resource);
            }
        }
    

        Route::resource('foos', 'FoosController')->except(['edit']);
    

        namespace App\Exceptions;

        use BaseTree\Exception\LaravelHandler as BaseTreeHandler;

        class Handler extends BaseTreeHandler
        {
        }
    

        'testing' => [
            'driver' => 'sqlite',
            'database' => ':memory:',
        ],
    

    namespace Tests\Models;

    use App\Models\Foo;
    use App\Models\Bar;
    use BaseTree\Tests\LaravelDatabaseTestCase;

    class FooTest extends LaravelDatabaseTestCase
    {
        /** @test */
        public function a_foo_has_one_bar()
        {
            $foo = create(Foo::class);
            $bar = create(Bar::class);

            $foo->bar()->save($bar);

            $this->assertHasOne($foo, $bar, 'bar', ['id' => $bar->id, 'foo_id' => $foo->id]);
        }
    }

    use BaseTree\Responses\JsonResponse;
    use App\Models\Foo;

    ...
    
    /** @test */
    public function it_should_fetch_all_foos(): void
    {
        $response = $this->jsonGet(route("foos.index"));
        $response->assertStatus(JsonResponse::HTTP_OK)->assertJsonStructure(['status', 'message', 'data']);
    }
    
    /** @test */
    public function it_ages assertions. Third argument is value that laravel will translate without the _
        $this->assertFieldRequired($validator, 'name');
        $this->assertEmailField($validator, 'user_email', 'user email');
        $this->assertPasswordIsConfirmed($validator, 'password');
        $this->assertValueIn($validator, 'value_from_enum', 'value from enum');
        $this->assertFieldExist($validator, 'id');
        $this->assertFieldIsArray($validator, 'array');
        $this->assertValueIsUnique($validator, 'unique_column', 'unique column');
    }
    
    /** @test */
    public function foo_can_be_stored(): void
    {
        $response = $this->jsonPost(route('foos.store', ['name' => 'Foo Name']));
        $response->assertStatus(JsonResponse::HTTP_UNPROCESSABLE_ENTITY)->assertJsonStructure([
            'status',
            'message',
            'validator'
        ]);
        
        $this->assertCreated(new Foo, ['name' => 'Foo Name']);
    }

    namespace App\BLL;
    
    use BaseTree\Resources\BaseResource;
    use App\DAL\FooRepository;
    
    class FooResource extends BaseResource
    {
        public function __construct(FooRepository $repository) 
        {
            parent::__construct($repository);
        }
        
        public function store(array $attributes) 
        {
            $attributes['slug'] = str_slug($attributes['name']);
            # Or whatever logic you need here
            
            return parent::store($attributes);
        }
        
    }

    Usage:
      php artisan basetree:dal [options]
    
    Options:
          --model[=MODEL]                              Fully qualified model name including the namespace
          --interface-folder[=INTERFACE-FOLDER]        Folder where to create the DAL interface [default: "app/DAL/[model-name]"]
          --interface-namespace[=INTERFACE-NAMESPACE]  Namespace to create the DAL interface under [default: "App\DAL\[model-name]"]
          --dal-folder[=DAL-FOLDER]                    Folder where to create the DAL implementation [default: "app/DAL/[model-name]"]
          --dal-namespace[=DAL-NAMESPACE]              Namespace to create the DAL implementation under [default: "App\DAL\[model-name]"]
    

    Usage:
      php artisan basetree:bll [options]
    
    Options:
          --model[=MODEL]                  Fully qualified model name including namespace
          --dal-interface[=DAL-INTERFACE]  Fully qualified data access layer name including namespace
          --folder[=FOLDER]                Folder where to create the BLL [default: "app/BLL/"]
          --namespace[=NAMESPACE]          Namespace to create the BLL under [default: "App\BLL"]
    

    Usage:
      php artisan basetree:controller [options]

    Options:
          --model-plural[=MODEL-PLURAL]  Plural form of the model name. For instance if the model is User, you should send here Users
          --bll[=BLL]                    Fully qualified business logic layer name including namespace
          --folder[=FOLDER]              Folder where to create the controller [default: "app/Http/Controllers/Api/"]
          --namespace[=NAMESPACE]        Namespace to create the controller under [default: "App\Http\Controllers\Api"]
    

    Usage:
      php artisan basetree:boilerplates [options]
    
    Options:
          --docker-compose  Publish the docker structure
 
    Help:
      Publish some already predefined environments.
          --docker-compose: Docker environment for local development (nginx 1.13, php7.1-fpm + composer, npm 3.3, nodejs 6.7, MariaDB 10.3, phpmyadmin 4.7)

    

            Name               Command                          State               Ports                
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
    tutorial_app_1         docker-php-entrypoint /sta ...       Up           443/tcp, 0.0.0.0:80->80/tcp, 9000/tcp
    tutorial_mariadb_1     docker-entrypoint.sh mysqld          Up           0.0.0.0:3307->3306/tcp               
    tutorial_phpmyadmin_1  /run.sh phpmyadmin                   Up           0.0.0.0:81->80/tcp 
    

        namespace App\Http\Controllers;
        
        use BaseTree\Controllers\Lumen\JsonController;
        use App\BLL\FooResource;
        
        class FoosController extends JsonController 
        {
            public function __construct(FooResource $resource) 
            {
                parent::__construct($resource);
            }
        }
    

        namespace App\Exceptions;

        use BaseTree\Exception\LumenHandler as BaseTreeHandler;

        class Handler extends BaseTreeHandler
        {
        }
    

        namespace Tests\Models;

        use App\Models\Foo;
        use App\Models\Bar;
        use BaseTree\Tests\LumenDatabaseTestCase;

        class FooTest extends LumenDatabaseTestCase
        {
            /** @test */
            public function a_foo_has_one_bar()
            {
                $foo = create(Foo::class);
                $bar = create(Bar::class);

                $foo->bar()->save($bar);

                $this->assertHasOne($foo, $bar, 'bar', ['id' => $bar->id, 'foo_id' => $foo->id]);
            }
        }