A typical Developer Blog
by Gordon Franke
Icon

What brings us Symfony2 2.2

Symfony2 version 2.2 Beta 2 is out so let us look what will it brings us.

Route

Use php function variable param default instate of route annotation config

/**
 * @Route("/hi/{name}", name="hi")
 */
public function hiAction($name = "Bob")
{
        return new Response($name);
}

see also: http://symfony.com/blog/new-in-symfony-2-2-small-things-matter

Static Page Cacheing

You can now specify the cache strategy

about:
    pattern: /about
    defaults:
        _controller: FrameworkBundle:Template:template
        maxAge: 86400
        sharedMaxAge: 86400
        private: false

see also: http://symfony.com/blog/new-in-symfony-2-2-cache-support-for-static-pages

Payment Validation

Luhn and CardScheme validator for e-commerce

Acme\SubscriptionBundle\Entity\Transaction:
    properties:
        cardNumber:
            - CardScheme:
                schemes: [VISA] # 10 schemes see symfony.com/doc/master/reference/constraints/CardScheme.html#schemes
                message: You credit card number is invalid.
        cardNumber:
            - Luhn:
                message: Please check your credit card number.

see also: http://symfony.com/blog/new-in-symfony-2-2-payment-related-validators

Security

Comparing Strings

use Symfony\Component\Security\Core\Util\StringUtils;
 
// is password1 equals to password2?
$bool = StringUtils::equals($password1, $password2);

Generating a secure Random Number

use Symfony\Component\Security\Core\Util\SecureRandom;
 
$generator = new SecureRandom();
$random = $generator->nextBytes(10);

INFO: also available as service `security.secure_random`

see also: http://symfony.com/blog/new-in-symfony-2-2-security-utilities

Debug

Fatal error catcher

Here is how a fatal error is displayed in Symfony 2.0 and 2.1:

And here is the same fatal error displayed from a Symfony 2.2 application:

minimize option

deprected calls

In your web debug toolbar

TIP: also available in your log file

see also: http://symfony.com/blog/new-in-symfony-2-2-nice-fatal-error-display
ses also: http://symfony.com/blog/new-in-symfony-2-2-small-things-matter
ses also: http://symfony.com/blog/new-in-symfony-2-2-logging-of-deprecated-calls

Twig

Schema relative urls usefull for mix content (https) and assets from cdn (http)

{{ url('blog', { post: 'what-a-wonderful-world' }, true) }}"

path relative urls (relative path based on the current URL) usefull for static page generation

{{ path('blog', { post: 'what-a-wonderful-world' }, true) }}

see also: http://symfony.com/blog/new-in-symfony-2-2-new-url-generation-options

Functional Tests

The profiler is disabled by default (performance improvement)

// Check that the profiler is enabled
if ($profile = $client->getProfile()) {
    $this->assertLessThan
(10, $profile->getCollector('db')->getQueryCount());
}

Enable the profiler with

$client->enableProfiler();

see also: http://symfony.com/blog/new-in-symfony-2-2-functional-tests-speed-up

Process Component

restart

You can now simple restart a process

use Symfony\Component\Process\ProcessBuilder;
 
if (!$process->isRunning()) {
    if (!$process->isSuccessful()) {
        $cloned = $process->restart();
 
        // ...
    }
}

status

More status check functions

$process->isSuccessful();
$process->hasBeenSignaled();
$process->hasBeenStopped();
$process->isRunning();
 
// new in 2.2
$process->isStarted();
$process->isTerminated();
 
if (!$process->isRunning()) {
    if ($process->isStarted() && !$process->isSuccessful()) {
        $cloned = $process->restart();
 
        // ...
    }
}

incremental process output

see also: http://symfony.com/blog/new-in-symfony-2-2-process-component-enhancements

Command line

Autocomplete

Try the following command and type your namespace ;)

app/console doctrine:generate:entity

TIP: using tab for use and arrows for change

Service Tagging informations

Get service information (includes tags list)

app/console container:debug security.firewall

Get all services tagged with `form.type`

app/console container:debug --tag=form.type

Alias support

Try the following command

app/console do:sc:dr --help

Try also an invalid commnd

app/console do:sc --help

User input choice list

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
 
$app = new Application();
$app->register('ask-color')->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
    $dialog = $app->getHelperSet()->get('dialog');
    $colors = array('red', 'blue', 'yellow');
 
    // simply ask for a color (no validation)
    $color = $dialog->ask($output, 'Enter your favorite color (default to red): ', 'red');
    $output->writeln('You have just entered: '.$color);
 
    // ask and validate the answer
    $color = $dialog->askAndValidate($output, 'Enter your favorite color (default to red): ', function ($color) use ($colors) {
        if (!in_array($color, array_values($colors))) {
            throw new \InvalidArgumentException(sprintf('Color "%s" is invalid.', $color));
        }
 
        return $color;
    }, false, 'red');
    $output->writeln('You have just entered: '.$color);
 
    // force the user to select from a pre-defined list of choices
    $color = $dialog->select($output, 'Please select your favorite color (default to red)', $colors, 0);
    $output->writeln('You have just selected: '.$colors[$color]);
});
 
$app->run();

Progress bar

$progress = $app->getHelperSet()->get('progress');
 
$progress->start($output, 50);
$i = 0;
while ($i++ < 50) {
    // do some work
    // advance the progress bar 1 unit
    $progress->advance();
}
$progress->finish();

Hidden password input

$dialog = $this->getHelperSet()->get('dialog');
$password = $dialog->askHiddenResponse($output, 'What is the database password?');

see also: http://symfony.com/blog/new-in-symfony-2-2-better-interaction-from-the-console
see also: http://symfony.com/blog/new-in-symfony-2-2-small-things-matter
see also: http://symfony.com/blog/new-in-symfony-2-2-autocomplete-on-the-command-line

NEW: StopWatch Component

Extacted from HttpKernel

Parameter Usage
eventName e.q. controller
categories use by symfony for different colors in the timeline
section usefull for grouping
use Symfony\Component\Stopwatch\Stopwatch;
 
$stopwatch = new Stopwatch();
 
$stopwatch->openSection();
 
// Start event named 'conroller' for category 'template'
$stopwatch->start('controller', 'template');
 
// ... some code goes here
$stopwatch->lap('controller');
 
// ... some code goes here
$event = $stopwatch->stop('controller');
 
$stopwatch->stopSection('routing');
 
// reopen
$stopwatch->openSection('routing');

see also: http://symfony.com/blog/new-in-symfony-2-2-new-stopwatch-component

NEW: PropertyAccess Component

Extracted from Form Component

use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
 
class DataGrid {
    public function __construct($items, array $columns, PropertyAccessorInterface $accessor) {
         // Store row data
         foreach ($items as $item) {
             $this->rows[] = array_map(function ($path) use ($item, $accessor) {
                 return $accessor->getValue($item, $path);
             }, $columns);
         }
    }
}
 
$accessor = PropertyAccess::getPropertyAccessor();
 
$data = array(
    array(
        'id' => 1,
        'firstName' => 'Paul',
        'instrument' => array(
            'name' => 'Guitar',
            'type' => 'String instrument',
        ),
    ),
);
 
$grid = new DataGrid($data, array(
    '[firstName]',
    '[instrument][name]',
), $accessor);
Path Equivalent to
[index] $data['index']
[index][sub] $data['index']['sub']
prop $data->getProp(), $data->isProp(), $data->hasProp(), $data->__get(‘prop’) or $data->prop, whichever is found first
prop.sub $data->getProp()->getSub(), $data->getProp()->isSub() etc.

Note: make sure to use one global accessor and inject it wherever you need it.
the symfony team work on a code generator to optimize the speed.

see also: http://symfony.com/blog/new-in-symfony-2-2-new-propertyaccess-component

Other stuff

The `_internal` route is not used any more. It should then be removed from both your routing and security configurations.

New `router_proxy` key under framework config for ESI and Hinclude.

see also: https://github.com/symfony/symfony-standard/blob/master/UPGRADE-2.2.md

SensioFrameworkExtraBundle

added the possibility to configure the repository method to use for the Doctrine converter via the `repository_method` option.

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
 
/**
 * @Route("/blog/{id}")
 * @ParamConverter("post", class="SensioBlogBundle:Post", options={"entity_manager" = "foo", "repository_method" = "findMyAll"})
 */
public function showAction(Post $post)
{
}

see also: https://github.com/sensio/SensioFrameworkExtraBundle/blob/master/CHANGELOG.md

Author:

Category: symfony, Uncategorized

Tagged: ,

Leave a Reply

CommentLuv badge