Docker containers (update docker images using watchtower)
I found this interesting article here
today and decide to implement it in my PHP project github repository
For those who already read my article, about some PHP8 new features implementation, the source code link is already available.
You just need to update the repository.
The latest PHP version is released on November 26, 2020. This version comes with a lot of new features and improvements.
In this article, I will implement some of these new features in an existing PHP project.
Project Link
You can download the code source from this github link
Php8 comes with a new expression match that replaces switch in the older versions.
match expression has a lot of advantages compared to traditional PHP switch cases.
Some of those advantages are
match expression offer shorter code block
it does not require break
there is no type coercion
Code implementation
Here, in this code, the ship parameter is passed through the createForm
The ship object type is instantiated depending on this parameter.
Older PHP version
publicfunctioncreateFromData(array $data): ?AbstractShip{
switch ($data['team']) {
case'empire':
$ship = (new Ship())
->setJediFactor($data['jedi_factor']);
break;
case'rebel':
$ship = new RebelShip();
break;
case'broken':
$ship = new BrokenShip();
break;
case'bounty hunter':
$ship = new BountyHunterShip();
break;
default:
$ship = (new Ship())
->setJediFactor($data['jedi_factor']);
}
}
What we got with PHP8 match
publicfunctioncreateFromData(array $data): ?AbstractShip{
if (!isset($data['team'])) {
returnnull;
}
$ship = match ($data['team']) {
'rebel' => new RebelShip(),
'broken' => new BrokenShip(),
'bounty hunter' => new BountyHunterShip(),
default => (new Ship())->setJediFactor($data['jedi_factor'])
};
}
It's been too long since my last post, on CQRS. Today I'm going to list some new features that Symfony 5 brings us.
Upgrade Symfony Version from 4.4 to 5
During the update of my project from Symfony 4.3 to Symfony 5, I learned a little detail about Composer.json file.
In fact, my composer.json file has an extra object in which I had specified the version of my current Symfony.
Theses few lines ("require": "4.3."), locked the Symfony version to the latest version of 4.3. This means that when you run composer update like this
composer update symfony/*
All updated versions of components will be the latest version of Symfony 4.3, which means 4.3.9. So with these lines of code, your composer update will never
update your Symfony version to 4.4.
Also, knowing that Symfony minor version never breaks backward compatibility, before upgrading to 5, it is necessary to upgrade 4.3 to 4.4. In this case,
you can be able to fix all deprecated before using a new major version.
CQRS, stand for Command query responsibility segregation, is a design pattern that involve a complete separation between reading
and writing data in your application. The command is responsible of writing data and the query stand for reading.
The first thing that come in mind when you read this little definition is:
Why do I need to implement this design pattern? Still, there is already CRUD pattern which stand for creating, reading, updating and deleting data.
As Martin Fowler explain it very well in his tutorial here, CQRS become very useful when
your are dealing with a very sophisticated behavior. beside, the CRUD pattern is not enough when we need to store data that's different
from the data we provided.
CQRS and Domain Driven Development implementation.
As, I'm passionate of using design pattern as soon as it is possible during my application development process, I join CQRS with Hexagonal Architecture.
pattern.
The little example is a template application. it's only goal is to be used for creating, deleting and updating template. The application architecture is
like this.
Application
CreateTemplateController
UpdateTemplateController
Domain
Command
CreateTemplate
CreateTemplateHandler
UpdateTemplate
UpdateTemplateHandler
Query
GetTemplateDataProvider
GetTemplateHandler
GetTemplateQuery
Infrastructure(nothing in this folder for this example)
Command part Code
All controllers are implemented following the one action, one controller pattern. This mean that in each controller, there is only
one magic method (__invoke), that implement all the logic.
In order to promote knowledge sharing within PHP community, AFUP organise every
year a PHP forum. This year, I was able to attend Paris's PHP Forum that took
place between 24th and 25th of october.
I'd like to share in this article the main topics of the last day of this forum.
Aggressive PHP Quality Insurance in 2019
During his presentation, Ocramus share with us some very useful tools to ensure code
quality in production
There are several tools to ensure code quality such as:
Psalm
Psalm is a static analysis tool for finding errors in PHP programs. You can install it
with composer
The doctrine coding standard is a PHP_CodeSniffer used on Doctrine projects.
The doctrine coding standard can be use to validate a set of rules specified to validate
Php code.
Basic usage
composer global require doctrine/coding-standard
phpcs --standard=Doctrine /path/to/some/file/to/sniff.php
phpcbf --standard=Doctrine /path/to/some/file/to/sniff.php
Symfony console component already implemented a feature to display tables in the console output. See the link below.
Display table in symfony Console
Symfony 4.4 added a new method in Table class to display horizontally(setHorizontale)
useSymfony\Component\Console\Command\Command;
useSymfony\Component\Console\Helper\Table;
useSymfony\Component\Console\Input\InputInterface;
useSymfony\Component\Console\Output\OutputInterface;
classSomeCommandextendsCommand{
publicfunctionexecute(InputInterface $input, OutputInterface $output){
$table = new Table($output);
$table
->setHeaders(['ISBN', 'Title', 'Author'])
->setRows([
['99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'],
['9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'],
['960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'],
['80-902734-1-6', 'And Then There Were None', 'Agatha Christie'],
])
->setHorizontal()
$table->render();
}
}
This will display table horizontally like:
| ISBN | 99921-58-10-7 | 971-5-0210-0 | 960-425-059-0 | 80-902734-1-6 |
| Title | Divine Comedy | A Tale of Two Cities | The Lord of the Rings | And Then There Were None |
| Author | Dante Alighieri | Charles Dickens | J. R. R. Tolkien' | Agatha Christie |
With SymfonyStyle, you can use the horizontal style directely;
When you are using Symfony Style, you can also display a list with Separator like this:
useSymfony\Component\Console\Helper\TableSeparator;
$io->definitionList(
['Version' => '4.4.0'],
['Long-Term Support' => 'Yes'],
new TableSeparator(),
'Timeline',
['End of maintenance' => '11/2022'],
['End of life' => '11/2023']
);
The output of this is:
----------------- --------
Version 4.4
Long-Term Support Yes
----------------- --------
TimeLine
End of maintenance 11/2022
End of life 11/2023
----------------- --------
Symfony 4.3 added a new feature (native password Encoder).
Because of the fast-paced nature of hashing algorithm, it becomes less and less recommended to select a default hashing algorithm.
Even the PHP function:
password_hash
use a default robust hashing algorithm to encrypt password more efficiently even if it also offer the
possibility to use the crypt algorithm for the hashing see documentation here
That's why Symfony 4.3 added a feature that allow to select auto hashing encoder in security configuration. That way, Symfony will
choose the more efficient algorithm during the encoding of the user password.
see the default Encoding implementation example below:
See the native Password Encoder documentation here
Now, Symfony 4.4 also bring the possibility to rehashed the password with a best existing hashing algorithm.
In practice the entity user provider will only need to implement PasswordUpgraderInterface.
JMAP is the new email protocol that address shortcomings of earlier e-mail
protocols and that is designed to fit modern age. It is a the protocol that offer
a new use to use and modern API for modern email clients.
JMAP Protocol in use
JMAP is developed by the large email developers community of IEFT. it offer an
an easy to use and modern API.
It is free and open source standard.
JMAP is robust, more general and more general protocol that handle a large
collection of use cases.