Environments
• Environment is not each machine you
running your project on
• Environment is each project configuration
you use
Slide 19
Slide 19 text
Environments
• You should be able to run your app using
each environment on your local machine
• So, please, be nice and keep env’s configs in
sync
• And put sensitive params to
parameters.yml or ignored local config
Slide 20
Slide 20 text
Coupling app to env
if ($this->container->getParameter('kernel.environment') == 'prod') {
$this->doSomethingCool();
}
Slide 21
Slide 21 text
Coupling app to env
namespace App\MainBundle\Service;
class Mailer
{
private $env
public function __construct($env)
{
$this->env = $env;
}
public function mail($to, $subject, $body)
{
if($this->env != 'prod') {
return;
}
// ...
}
}
Slide 22
Slide 22 text
Decoupling app from env
namespace App\MainBundle\Service;
class Mailer
{
private $disableDelivery
public function __construct($disableDelivery)
{
$this->disableDelivery = $disableDelivery;
}
public function mail($to, $subject, $body)
{
if($this->disableDelivery) {
return;
}
// ...
}
}
Slide 23
Slide 23 text
The application should not be aware of what
environment it is running under, it should just be
configured a particular way based on its configuration
files.
http://richardmiller.co.uk/2013/05/28/symfony2-avoiding-coupling-applications-to-the-environment/
http://www.flickr.com/photos/ezu/73784031/
Slide 24
Slide 24 text
Cache and Logs
Slide 25
Slide 25 text
Cache and logs
/**
* change caching directory for the system's -
* optimization for local/test/dev
*
* @return string
*/
public function getCacheDir()
{
if (in_array(
$this->environment,
array('dev','test','local'))
) {
return sys_get_temp_dir()
. '/cache/'
. $this->environment;
}
return parent::getCacheDir();
}
Slide 26
Slide 26 text
Cache and logs
/**
* change caching directory for the system's -
* optimization for local/test/dev
*
* @return string
*/
public function getCacheDir()
{
if (in_array(
$this->environment,
array('dev','test','local'))
) {
return sys_get_temp_dir()
. '/cache/'
. $this->environment;
}
return parent::getCacheDir();
}
Slide 27
Slide 27 text
Cache and logs
Slide 28
Slide 28 text
Cache and logs
How many times you just set umask(0000)?
http://symfony.com/doc/current/book/
installation.html#configuration-and-setup
Annoying Composer?
In a SF2 bundle:
"require": { "zendframework/zendframework": "dev-master" }
Slide 36
Slide 36 text
Annoying Composer?
Slide 37
Slide 37 text
Annoying Composer?
• Add your composer.lock to .gitignore
Slide 38
Slide 38 text
Annoying Composer?
• Add your composer.lock to .gitignore
• ...
Slide 39
Slide 39 text
Annoying Composer?
• Add your composer.lock to .gitignore
• ...
• Profit!
Slide 40
Slide 40 text
Annoying Composer?
• Add your composer.lock to .gitignore
• ...
• Profit!
• Wait, no. Have fun, bitches!
Slide 41
Slide 41 text
Let’s change vendors!
Edit vendor/sonata-project/user-bundle/Sonata/UserBundle/
Model/User.php. Change line:
use FOS\UserBundle\Entity\User as AbstractedUser;
to
use FOS\UserBundle\Document\User as AbstractedUser;
Slide 42
Slide 42 text
Let’s change vendors!
Slide 43
Slide 43 text
The vendor directory is a black box. The public API is
vendor/autoload.php.
https://igor.io/2013/09/04/composer-vendor-directory.html
http://www.flickr.com/photos/ezu/73784031/
Slide 44
Slide 44 text
Update blindly
$ composer update
Slide 45
Slide 45 text
Update blindly
$ composer update
Now pray.
Slide 46
Slide 46 text
The upside of OSS is that you are in charge, and you
actually can fix things when they break. Also, you can
prevent them from breaking in the first place.
https://igor.io/2013/09/24/dependency-responsibility.html
http://www.flickr.com/photos/ezu/73784031/
Extended
class RubricService
{
public function __construct($security, ...)
{
$this->user = $security->getToken()->getUser();
...
}
}
Slide 58
Slide 58 text
ACL
Slide 59
Slide 59 text
ACL
Slide 60
Slide 60 text
ACL
• Don’t use ACL
Slide 61
Slide 61 text
ACL
• Don’t use ACL
• Common, just never don’t
Slide 62
Slide 62 text
ACL
• Don’t use ACL
• Common, just never don’t
• Even if somebody asks you to
Slide 63
Slide 63 text
ACL
• Don’t use ACL
• Common, just never don’t
• Even if somebody asks you to
• Just. Don’t. Use. ACL
Slide 64
Slide 64 text
ACL
• Don’t use ACL
• Common, just never don’t
• Even if somebody asks you to
• Just. Don’t. Use. ACL
• Another bullet about not using ACL
Slide 65
Slide 65 text
Why?
Slide 66
Slide 66 text
Why?
Because Voters!
Slide 67
Slide 67 text
Voters
• Can user edit the record?
• Is he the author of it?
• Does he have
Roles:ROLE_SUPER_ADMIN ?
• No? Then screw you! Access denied!
Slide 68
Slide 68 text
Voters
public function editAction($shopId)
{
$shop = // get the Shop entity
...
if (!$this->sc->isGranted("EDIT", $shop)) {
throw new AccessDeniedExceptions);
}
}
Slide 69
Slide 69 text
Voters
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class ShopVoter implements VoterInterface
{
public function supportsAttribute($attribute)
{
return in_array($attribute, array(
"EDIT", "MANAGE_ADMINS", "DELETE")
);
}
public function supportsClass($class)
{
return $class == "App\AppBundle\Entity\Shop";
}
}
Slide 70
Slide 70 text
Voters
public function vote(TokenInterface $token, $object, array $attributes)
{
if (!$this->supportObject($object)) {
return VoterInterface::ACCESS_ABSTAIN;
}
$accessToken = $this->container->get('request')->getUser();
try {
$user = $this->getRepository('AppBundle:User')
->findUserByAccessToken($accessToken);
if (!$user->getToken()->isActive()) {
return VoterInterface::ACCESS_DENIED;
}
} catch (\Doctrine\ORM\NoResultException $e) {
return VoterInterface::ACCESS_DENIED;
}
...
}
Slide 71
Slide 71 text
Voters
public function vote(TokenInterface $token, $object, array $attributes)
{
...
if ($user->hasRole(UserRole::ROLE_ADMIN)) {
return VoterInterface::ACCESS_GRANTED;
}
if ($object->getAdmins()->contains($user)) {
return VoterInterface::ACCESS_GRANTED;
}
return VoterInterface::ACCESS_DENIED;
}
Slide 72
Slide 72 text
General stuff
http://www.flickr.com/photos/anitacanita/756830136/
Slide 73
Slide 73 text
Naming
Slide 74
Slide 74 text
Naming
• instead of NewUserEventListener use
SendConfirmationListener
• {name of the bundle without “Bundle”}.
{name of the controller without
“Controller”}.{name of the action without
“Action”}
• Stay consistent
Slide 75
Slide 75 text
Code generation inside
generate
generate:bundle Generates a bundle
generate:controller Generates a controller
generate:doctrine:crud Generates a CRUD based on a ...
generate:doctrine:entities Generates entity classes and ...
generate:doctrine:entity Generates a new Doctrine ...
generate:doctrine:form
Slide 76
Slide 76 text
No $request->get()
public function get($key, $default = null, $deep = false)
{
return $this->query->get(
$key, $this->attributes->get(
$key, $this->request->get(
$key, $default, $deep)
, $deep)
, $deep
);
}