PHPers Poznań #3
23.02.2016
Symfony
tips & tricks
http://www.slideshare.net/javier.eguiluz/new-symfony-tips-tricks-symfonycon-paris-2015
Accessing
request
parameters
/**
* @param Request $request
*
* @return JsonResponse
*/
public function getCandidateVisitAction(Request $request)
{
$candidateId = (int) $request->get('id');
$candidates = $this->getDoctrine()
->getRepository('EspeoCommonBundle:CandidateVisit')
->getVisitStatistics($candidateId);
return new JsonResponse($candidates);
}
/**
* @param Request $request
*
* @return JsonResponse
*/
public function getCandidateVisitAction(Request $request)
{
$candidateId = $request->query->getInt('id');
$candidates = $this->getDoctrine()
->getRepository('EspeoCommonBundle:CandidateVisit')
->getVisitStatistics($candidateId);
return new JsonResponse($candidates);
}
Naming
strategies
/**
* CandidateVisit.
*
* @ORMTable(name="candidate_visit")
* @ORMEntity(repositoryClass="EspeoCommonBundleEntity
RepositoryCandidateVisitRepository")
*/
class CandidateVisit
{
/**
* @var DateTime
*
* @ORMColumn(name="date_visit", type="datetime")
*/
private $dateVisit;
}
# app/config/config.yml
doctrine:
dbal:
(...)
orm:
naming_strategy: doctrine.orm.naming_strategy.underscore
<?php
namespace DoctrineORMMapping;
interface NamingStrategy
{
function classToTableName($className);
function propertyToColumnName($propertyName, $className = null);
function referenceColumnName();
function joinColumnName($propertyName);
function joinTableName($sourceEntity, $targetEntity, $propertyName);
function joinKeyColumnName($entityName, $referencedColumnName = null);
}
Twig &
app.user
{% if app.user and is_granted('ROLE_ADMIN') %}
...
{% endif %}
Email
delivery
whitelist
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool:
type: file
path: "%kernel.root_dir%/spool"
# app/config/config_dev.yml
swiftmailer:
delivery_address: dev@example.com
delivery_whitelist:
# all email addresses matching these regexes will be delivered
# like normal, as well as being sent to dev@example.com
- '/@specialdomain.com$/'
- '/^admin@mydomain.com$/'
Automatic
"smoke testing"
for services
public function testContainerServices()
{
$client = static::createClient();
foreach ($client->getContainer()->getServiceIds() as $serviceId) {
$service = $client->getContainer()->get($serviceId);
$this->assertNotNull($service);
}
}
public function testContainerServices()
{
$client = static::createClient();
foreach ($client->getContainer()->getServiceIds() as $serviceId) {
try {
$startedAt = microtime(true);
$service = $client->getContainer()->get($serviceId);
$elapsed = (microtime(true) - $startedAt) * 1000;
$this->assertLessThan(50, $elapsed);
} catch(InactiveScopeException $e) {
}
}
}
symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
ParamConverter
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SensioBundleFrameworkExtraBundleConfigurationParamConverter;
/**
* @Route("/blog/{id}")
* @ParamConverter("post", class="SensioBlogBundle:Post")
*/
public function showAction(Post $post)
{
}
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SensioBundleFrameworkExtraBundleConfigurationParamConverter;
/**
* @Route("/blog/{id}")
* @ParamConverter("post", class="SensioBlogBundle:Post", options=
{"entity_manager" = "foo"})
*/
public function showAction(Post $post)
{
}
Bundle
http://jmsyst.com/bundles/JMSDiExtraBundle
JMSDiExtraBundle
// composer.json
{
// ...
require: {
// ...
"jms/di-extra-bundle": "dev-master"
}
}
#app/AppKernel.php
$bundles = array(
// ...
new JMSDiExtraBundleJMSDiExtraBundle($this),
new JMSAopBundleJMSAopBundle(),
// ...
);
#app/config/config.yml
jms_di_extra:
locations:
all_bundles: false
bundles: [FooBundle, AcmeBlogBundle]
directories: ["%kernel.root_dir%/../src"]
<?php
namespace EspeoCandidateBundleController;
use EspeoCandidateBundleServiceCalendarEventProvider;
use EspeoCommonBundleControllerController;
use EspeoCommonBundleEntityRepositoryCitiesRepository;
use JMSDiExtraBundleAnnotation as DI;
use SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorage;
use SymfonyComponentTranslationTranslatorInterface;
class CalendarApiController extends Controller
{
/**
* @var CalendarEventProvider
* @DIInject("espeo_candidate.calendar_event_provider")
*/
private $calendarEventProvider;
/**
* @var TokenStorage
* @DIInject("security.token_storage")
*/
private $tokenStorage;
/**
* @var CitiesRepository
* @DIInject("espeo_common.cities_repository")
*/
private $citiesRepository;
}
<?php
class CalendarApiController extends Controller
{
/**
* @var CalendarEventProvider
* @DIInject("espeo_candidate.calendar_event_provider")
*/
private $calendarEventProvider;
/**
* @var TokenStorage
* @DIInject("security.token_storage")
*/
private $tokenStorage;
/**
* @var CityRepository
* @DIInject("espeo_common.city_repository")
*/
private $cityRepository;
}
private function getCoolCities()
{
$cities = $this->cityRepository->getSosnowiecAndRadom();
return $cities;
}
use JMSDiExtraBundleAnnotation as DI;
class Controller
{
private $em;
private $session;
/**
* @DIInjectParams({
* "em" = @DIInject("doctrine.orm.entity_manager"),
* "session" = @DIInject("session")
* })
*/
public function __construct($em, $session)
{
$this->em = $em;
$this->session = $session;
}
}
https://github.com/willdurand/BazingaJsTranslationBundle
BazingaJsTranslation
Bundle
composer require "willdurand/js-translation-bundle"
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new BazingaBundleJsTranslationBundleBazingaJsTranslationBundle()
);
}
#app/config/routing.yml
_bazinga_jstranslation:
resource: "@BazingaJsTranslationBundle/Resources/config/routing/routing.yml"
#app/config/assetic/js.yml
bazinga_js_translation_js:
inputs:
- '%kernel.root_dir%/../web/bundles/bazingajstranslation/js/translator.min.js'
- '%kernel.root_dir%/../web/js/translations/config.js'
- '%kernel.root_dir%/../web/js/translations/*/*.js'
compile:
php app/console bazinga:js-translation:dump
php app/console assetic:dump
{% javascripts '@bazinga_js_translation_js' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
https://github.com/boxuk/angular-symfony-translation
angular-symfony-
translation
bower install --save angular-symfony-translation
#app/config/assetic/js.yml
bazinga_js_angular_js:
inputs:
- '%kernel.root_dir%/../bower_components/angular-symfony-translation/
dist/angular-symfony-translation.js'
var app = angular.module('my_module', [
'boxuk.translation'
]);
{% javascripts '@bazinga_js_translation_js' '@bazinga_js_angular_js' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
[[ ('errand.status.in_progress') | trans ]]
#src/Espeo/CommonBundle/Resources/translations/messages.pl.yml
errand:
status:
in_progress: W trakcie
closed: Zamknięte
canceled: Odwołany
new: Nowy
ESPEO SOFTWARE
Mariusz Kozłowski
mariusz.kozlowski@espeo.eu

Symfony tips and tricks