Recursively delete directories Gau Frette FTP adapter

To with FTP recursively delete files and directories adapter Gau Frette, you can apply following trick to avoid the error message:

ftp_rmdir(): Directory not empty.

1. just delete all files

2. then delete the lowest directories up to the top:

/**
 * @param Filesystem $fileSystem
 */
function deleteAllFilesInDirectory(Filesystem $fileSystem)
{
    // delete files first, than directories
    foreach ($fileSystem->keys() as $key) {
        if (!$fileSystem->isDirectory($key)) {
            $fileSystem->delete($key);
        }
    }
    $keys = $fileSystem->keys();
    usort($keys, function (string $a, string $b){
        $aCount = substr_count($a, '/');
        $bCount = substr_count($b, '/');
        return $bCount <=> $aCount;
    });
    foreach ($keys as $key) {
        $fileSystem->delete($key);
    }
}

Symfony FosUser bundle to apply their own password policy

In the SonataAdmin with FosUser bundle your own rules to define module for the strength of the passwords, can you overwrite the validation.xml, by one in the folder src/application/Sonata/UserBundle/resources/config /. its own validation.XML creates:

<?xml version="1.0" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
        http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

    <class name="FOS\UserBundle\Model\User">

        <property name="plainPassword">
            <constraint name="NotBlank">
                <option name="message">fos_user.password.blank</option>
                <option name="groups">
                    <value>Registration</value>
                    <value>ResetPassword</value>
                    <value>ChangePassword</value>
                </option>
            </constraint>
            <constraint name="Length">
                <option name="min">8</option>
                <option name="max">50</option>
                <option name="minMessage">fos_user.password.short</option>
                <option name="groups">
                    <value>Registration</value>
                    <value>Profile</value>
                    <value>ResetPassword</value>
                    <value>ChangePassword</value>
                </option>
            </constraint>
            <constraint name="Regex">
                <option name="pattern">/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/</option>
                <option name="message">Das Passwort muss mindestens 8 Zeichen haben, eine Zahl, GroƟ und Kleinschreibung enthalten.</option>
                <option name="groups">
                    <value>Registration</value>
                    <value>Profile</value>
                    <value>ResetPassword</value>
                    <value>ChangePassword</value>
                </option>
            </constraint>
        </property>
    </class>
</constraint-mapping>

Create symfony brute-force guard for SonataAdmin

For the SonataAdmin bundle There is the possibility of Symofny guards own logic in the login process to incorporate, such as:

  • Defense against brute force attacks by a maximum number of login attempts

In the following example, I have configured a guard for the administration area, It will count, How many times a user incorrectly logged.

The UserManager must then contain the logic, to counting the number of logins for each user, and it formed. also to ban for a period of time. more…

Working Gau Frette with file streams

To with Gau Frette (Symfony extension) to be able to perform also stream operations on files, can you get the little scrawny documented Stream wrapper use.

This is configured in the gaufrette.yaml:

knp_gaufrette:
    stream_wrapper: ~
    filesystems:
        backup1:
            adapter: backup

And can then be z.B. the following mass is a csv. Write file:

$stream = fopen('gaufrette://backup1/datei.csv', 'w+');

fputcsv($stream, [1,2,3]);

Dynamically configure symfony service ID using parameters

If you like in his Depnedency configuration of injection (services.yml) the service injections make dynamically configurable wants about the parameters.yml,

parameters:
    my_class: 'App\MyClass'

can you do this using the Symfony expression language component do:

composer require symfony/expression-language

Then you can define in the services.yml:

services:
    App\Command\MyCommand:
      arguments:
        - '@=service(parameter("my_class"))'

Yii2 versus symfony 4 – a framework comparison

I have long worked with Yii and the symfony framework and want to try the following to explain the differences between the two frameworks from programmers point of view.

Configuration of environments

When you create Web applications is typically at least 2 Environments: The development environment, on which you want to see errors and the live environment, where this should happen. To come test environment to run automated tests and perhaps a staging environment.

Symfony raises the very skillfully, with the .env files and own folders within the configuration folder.

At Yii must the programmers themselves lend a hand, to load the correct configuration, There are only from home 3 (dev, Prod, test) Vorgsehen environments, then it is confusing. more…

Twig extension to sort of entities by DateTime property

If one wants to sort a collection of doctrine in the template after a time stamp (createdAt the example), before actually make this.

If this is not possible, e.g.. in the Sonata Admin Bundle, then you can use this twig extension:

{% foo| sortByCreatedAt('asc') %}

Twig extension code:

<?php

namespace App\Twig;

use App\Entity\Tag;
use Doctrine\ORM\PersistentCollection;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class AppExtension extends AbstractExtension
{
    public function getFilters()
    {
        return array(
            new TwigFilter('sortByCreatedAt', array($this, 'sortByCreatedAt')),
        );
    }

    /**
     * @param PersistentCollection $objects
     * @return mixed
     */
    public function sortByCreatedAt($objects, $direction = 'asc')
    {
        $objects = $objects->toArray();
        usort($objects, function ($a, $b) use($direction) {
            if ($direction === 'asc') {
                return $a->getCreatedAt() >  $b->getCreatedAt();
            } elseif ($direction === 'desc') {
                return $a->getCreatedAt() <  $b->getCreatedAt();
            } else {
                throw new \Exception('unknown sort direction');
            }

        });
        return $objects;
    }
}

Strato und MySQL: General error: 1709 Index column size too large. The maximum column size is 767 bytes.

A customer following error message was shown to me, When I tried the database have symfony to create:

General error: 1709 Index column size too large. The maximum column size is 767 bytes.

This is because, the Strato strange setting in their managed hosting packages such as z.B. has the STRATO PowerWeb. Strato unfortunately won't change this setting, but you can in symfony in the doctrine.yaml (config.YAML) change the charset, Symfony works well on a Strato Server:

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.6'
        charset: utf8
        default_table_options:
            charset: utf8
            collate: utf8_general_ci

Symfony security passwords hash with the PasswordEncoder

The PasswordEncoder the symfony framework secure hashes of passwords in the database is very well suitable also in future to save and configure Central.

The SecurityBundle must if necessary. to be installed:

composer require symfony/security-bundle

Putting this in the security.yaml, you want to use what hashing algorithm for which entity:

security:
    encoders:
        App\Entity\User: bcrypt

The entity must implement the UserInterface: more…

Symfony 3 Establish a test database for integration testing

In Symofny it is called automatically when you run tests app/config/parameters_test.yml loaded.
There, another database should then be specified, for simplicity sake with the same database user:

database_host: same_as_dev
database_port: same_as_dev
database_name: test_db
database_user: same_as_dev
database_password: same_as_dev

Then following commands must be executed on the console:
Clear cache:
am PHP / console cache:clear –env = test

Create database
am PHP / console doctrine:database:create –env = test

Create tables
HP am / console doctrine:schema:update –env = test –force

Then can fixtures are loaded and integration tests are written.