railken / laravel-manager
Installs: 5 144
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=8.2
- ext-intl: *
- nikic/php-parser: ^4.18
- railken/bag: ^2.0.0
- respect/validation: ^2.2
- symfony/validator: 6.* || 7.*
- symfony/yaml: 6.* || 7.*
Requires (Dev)
- dev-master
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v3.3.7
- v3.3.6
- v3.3.5
- v3.3.4
- v3.3.3
- v3.3.2
- v3.3.1
- v3.3.0
- v3.2.71
- v3.2.70
- v3.2.69
- v3.2.66
- v3.2.65
- v3.2.64
- v3.2.63
- v3.2.62
- v3.2.61
- v3.2.60
- v3.2.59
- v3.2.58
- v3.2.57
- v3.2.56
- v3.2.55
- v3.2.54
- v3.2.53
- v3.2.52
- v3.2.51
- v3.2.50
- v3.2.49
- v3.2.48
- v3.2.47
- v3.2.46
- v3.2.45
- v3.2.44
- v3.2.43
- v3.2.42
- v3.2.41
- v3.2.40
- v3.2.39
- v3.2.38
- v3.2.37
- v3.2.36
- v3.2.35
- v3.2.34
- v3.2.33
- v3.2.32
- v3.2.31
- v3.2.30
- v3.2.29
- v3.2.28
- v3.2.27
- v3.2.26
- v3.2.25
- v3.2.24
- v3.2.23
- v3.2.22
- v3.2.21
- v3.2.20
- v3.2.19
- v3.2.18
- v3.2.17
- v3.2.16
- v3.2.15
- v3.2.14
- v3.2.13
- v3.2.12
- v3.2.11
- v3.2.10
- v3.2.9
- v3.2.8
- v3.2.7
- v3.2.6
- v3.2.5
- v3.2.4
- v3.2.3
- v3.1.19
- v3.1.18
- v3.1.17
- v3.1.16
- v3.1.15
- v3.1.14
- v3.1.13
- v3.1.12
- v3.1.11
- v3.1.10
- v3.1.9
- v3.1.8
- v3.1.7
- v3.1.6
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.8.0
- v2.7.0
- v2.6.9
- v2.6.8
- v2.6.7
- v2.6.6
- v2.6.5
- v2.6.4
- v2.6.3
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.3
- v2.5.2
- v2.5.1
- v2.5.0
- v2.4.0
- v2.3.6
- v2.3.5
- v2.3.4
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.0
- v1.0.1
- v1.0.0
This package is auto-updated.
Last update: 2024-11-09 06:23:13 UTC
README
A precise way to structure your logic to improve readability and maintainability of your code when building API.
Requirements
PHP 8.1 and later.
Installation
You can install it via Composer by typing the following command:
composer require railken/lem
The package will automatically register itself.
Usage
First you need to generate a new structure folder, use:
php artisan railken:make:manager app App\Foo
.
Now you can use it.
use App\Foo\FooManager; $manager = new FooManager(); $result = $manager->create(['name' => 'foo']); if ($result->ok()) { $foo = $result->getResource(); } else { $result->getErrors(); // All errors go here. }
How can you get an Error during an operation? An error occurs when a validation or authorization fails. The cool thing about it is that you have the total control during each process: using Validator and Authorizer. When you're retrieving errors you're receiving a Collection, it goes pretty well when you're developing an api. Here's an example
$manager = new FooManager(); $result = $manager->create(['name' => 'f')); print_r($result->getErrors()->toArray()); /* Array ( [0] => Array ( [code] => FOO_TITLE_NOT_DEFINED [attribute] => title [message] => The title is required [value] => ) [1] => Array ( [code] => FOO_NAME_NOT_DEFINED [attribute] => name [message] => The name isn't valid [value] => f ) ) */
So, what about the authorization part? First we have to edit the class User.
use Railken\Lem\Contracts\AgentContract; class User implements AgentContract { public function can($permission, $arguments = []) { return true; } }
You can set the method can as you wish, it's better if a permission library is used such as https://github.com/Zizaco/entrust or https://github.com/spatie/laravel-permission.
If no system permission is needed simply leave return true.
use Railken\Lem\Agents\SystemAgent; $manager = new FooManager(Auth::user()); $result = $manager->create(['title' => 'f']); print_r($result->getErrors()->toArray()); /* Array ( [0] => Array ( [code] => FOO_TITLE_NOT AUTHORIZED [attribute] => title [message] => You're not authorized to interact with title, missing foo.attributes.title.fill permission [value] => ) ) *
"foo.attributes.title.fill" is passed to User::can() and if the return is false the result will contains errors.
Note: if you don't set any agent, the SystemAgent will be used (all granted).
See Authorizer for more explanations.
Commands
- Generate a new set of files
php artisan railken:make:manager [path] [namespace]
. E.g. php artisan railken:make:manager App "App\Foo"
Manager
This is the main class, all the operations are performed using this: creating, updating, deleting, retrieving. This class is composed of components which are: validator, repository, authorizer, parameters, serializer
See Manager.
namespace App\Foo; use Railken\Lem\Manager; use Railken\Lem\Contracts\AgentContract; class FooManager extends Manager { /** * Class name entity * * @var string */ public $entity = Foo::class; /** * Construct * * @param AgentContract|null $agent */ public function __construct(AgentContract $agent = null) { parent::__construct($agent); } }
Model
This is the Eloquent Model, nothing changes, excepts for an interface
namespace App\Foo; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Railken\Lem\Contracts\EntityContract; class Foo extends Model implements EntityContract { use SoftDeletes; /** * The table associated with the model. * * @var string */ protected $table = 'foo'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = ['name']; /** * The attributes that should be mutated to dates. * * @var array */ protected $dates = ['deleted_at']; }
Repository
This is a Repository, the concept is very similar to the Repository of Symfony, code all your queries here.
See Repository for more information.
namespace App\Foo; use Railken\Lem\Repository; class FooRepository extends Repository { /** * Custom method * * @param string $name * * @return Foo */ public function findOneByName($name) { return $this->findOneBy(['name' => $name]); } }
Validator
Here comes the validator, and again it's very simple. validate() is called whenever a create/update operation is called. Remember: always return the collection of errors. You can of course add a specific library for validation and use it here.
namespace App\Foo; use Railken\Lem\Contracts\EntityContract; use Railken\Lem\Contracts\ValidatorContract; use Railken\Lem\ParameterBag; use Illuminate\Support\Collection; use App\Foo\Exceptions as Exceptions; class FooValidator implements ValidatorContract { /** * Validate * * @param EntityContract $entity * @param ParameterBag $parameters * * @return Collection */ public function validate(EntityContract $entity, ParameterBag $parameters) { $errors = new Collection(); if (!$entity->exists) $errors = $errors->merge($this->validateRequired($parameters)); $errors = $errors->merge($this->validateValue($entity, $parameters)); return $errors; } }
Serializer
This class will serialize your model
namespace App\Foo; use Railken\Lem\Contracts\SerializerContract; use Railken\Lem\Contracts\EntityContract; use Illuminate\Support\Collection; use Railken\Bag; class FooSerializer implements SerializerContract { /** * Serialize entity * * @param EntityContract $entity * * @return Bag */ public function serialize(EntityContract $entity, Collection $select) { $bag = (new Bag($entity->toArray()))->only($select->toArray()); return $bag; } }