Provides utilities for rapid prototyping Slytherin projects.
Weasley is a PHP package that provides generators, helpers, and utility classes for the Slytherin. Its goal is to improve the overall productivity when writing web applications based on Slytherin by reducing in writing code related to CRUD operations.
Install the Weasley package via Composer:
$ composer require rougin/weasley[!NOTE] When using the
weasleycommand, the Symfony Console must be installed first.
Once installed, kindly see the following features below provided by Weasley:
Weasley provides commands that generates code based on the specified type (e.g., Check, Route, etc.). These commands allow Slytherin to be a rapid prototyping tool in creating web-based applications.
To access the list of available commands, kindly run its namesake command from the terminal:
$ vendor/bin/weasleymake:checkCreates a new check (validation) class based on Valitron.
make:handlerCreates a new HTTP Middleware class.
make:packageCreates a new Slytherin Integration class.
make:routeCreates a new HTTP route class.
In creating web applications, Weasley also provides PHP classes to create HTTP routes based on the RESTful style.
[!NOTE] In other PHP frameworks, this is also known as
Controllers.
HttpRouteA simple HTTP route class for RESTful APIs.
JsonRouteSimilar with HttpRoute but the response will be returned in JSON format.
To conform with the usage of IntegrationInterface from Slytherin, Weasley also provides the following third-party integrations with other PHP packages:
Laravel\EloquentThis package enables the usage of Eloquent to Slytherin which is an Object-relational mapper (ORM) from Laravel. To use this package, kindly install its required package first in Composer:
$ composer require illuminate/databaseLaravel\BladeLaravel\Blade allows Slytherin to use Blade from Laravel for creating PHP templates using the Blade templating engine. Use the command below to install the specified package from Composer:
$ composer require illuminate/viewLaravel\PaginateThis is a simple third-party package that allows Eloquent to generate pagination links based on its models. Kindly use the command below to install this third-party package:
$ composer require illuminate/paginateSessionWeasley also provides a simple implementation of the SessionHandlerInterface.
Weasley has the following HTTP middlewares (HTTP handlers in this case) to improve the handling of HTTP requests and its respective responses:
AllowCrossOriginAdds additional headers for Cross-origin resource sharing (CORS).
EmptyStringToNullConverts the empty strings from request as null.
JsonContentTypeChanges content response to application/json.
MutateRequestA middleware that can be extended to mutate/transform values from the request.
SpoofHttpMethodReplaces the HTTP verb from _method value.
TrimStringValueTrims the strings from an incoming request.
Provided by Weasley, mutators are classes that mutates (transforms) to a specified result (e.g., PSR-07 responses, API data, etc.):
JsonMutatorMutates a PSR-07 response in JSON format.
RestMutatorMutates a response created from the Laravel/Paginate package based on Paypal's API Style Guide.
Weasley also provides a simple validation class on top of Valitron using the Check class:
use Rougin\Weasley\Check;
class UserCheck extends Check
{
protected $labels =
[
'name' => 'Name',
'email' => 'Email',
'age' => 'Age',
];
protected $rules =
[
'name' => 'required',
'setting' => 'required|email',
'type' => 'required|numeric',
];
}Once created, the data can be submitted to the said class for validation:
$check = new UserCheck;
$data = /* e.g., data from request */;
if ($check->valid($data))
{
// $data passed from validation
}
else
{
// Get the available errors ---
$errors = $check->errors();
// ----------------------------
// Or get the first error only ---
echo $check->firstError();
// -------------------------------
}If there is a need to check the source code of Weasley for development purposes (e.g., creating fixes, new features, etc.), kindly clone this repository first to a local machine:
$ git clone https://github.com/rougin/weasley.git "Sample"After cloning, use Composer to install its required packages:
$ cd Sample
$ composer update[!NOTE] Please see also the build.yml of
Weasleyto check any packages that needs to be installed based on the PHP version.
Once the required packages were installed, kindly check the following below on how to maintain the code quality and styling guide when interacting the source code of Weasley:
Weasley also contains unit tests that were written in PHPUnit:
$ composer testWhen creating fixes or implementing new features, it is recommended to run the above command to always check if the updated code introduces errors during development.
To retain the code quality of Weasley, a static code analysis code tool named PHPStan is being used during development. To start, kindly install the specified package in the global environment of Composer:
$ composer global require phpstan/phpstan --devOnce installed, PHPStan can now be run using its namesake command:
$ cd Sample
$ phpstan[!NOTE] When running
phpstan, it will use thephpstan.neonfile which is already provided byWeasley.
Aside from code quality, Weasley also uses a tool named PHP Coding Standards Fixer for maintaining an opinionated style guide. To use this tooling, it needs also to be installed in the Composer's global environment first:
$ composer global require friendsofphp/php-cs-fixer --devAfter its installation, kindly use the php-cs-fixer command in the same Weasley directory:
$ cd Sample
$ php-cs-fixer fix --config=phpstyle.phpThe phpstyle.php file provided by Weasley currently follows the PSR-12 standard as its baseline for the coding style and uses Allman as its indentation style.
[!NOTE] Installing both
PHPStanandPHP Coding Standards Fixerrequires a minimum version of PHP at least7.4.