Slide 1

Slide 1 text

Как превратить проект на Symfony в боль Roman Lapin Evercode Lab @memphys [email protected]

Slide 2

Slide 2 text

Who is this guy? • Web developer since 2005 • Evercode Lab – Co-founder and CEO • Using Symfony2 since 2011 • Love foosball and comics

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Viewers Discretion is Advised

Slide 5

Slide 5 text

Where does the pain come from?

Slide 6

Slide 6 text

Why use frameworks? http://www.flickr.com/photos/doberagi/1404539812/

Slide 7

Slide 7 text

Why use frameworks? • Code Organization • Existing wheels • Faster development • Community • Focus on your business logic, not core features

Slide 8

Slide 8 text

Why use Symfony2?

Slide 9

Slide 9 text

Why use Symfony2?

Slide 10

Slide 10 text

Project Structure http://www.flickr.com/photos/kwl/8642451160/

Slide 11

Slide 11 text

Project Structure !"" app # !"" AppKernel.php # !"" cache # !"" config # !"" logs | $"" ... !"" bin !"" composer.json !"" composer.lock !"" src # $"" Acme !"" vendor $"" web !"" app.php !"" app_dev.php $"" ...

Slide 12

Slide 12 text

Project Structure !"" app # !"" autoload.php # !"" AppCache.php # !"" AppKernel.php # !"" config # $"" Resources !"" bin # !"" console # $"" symfony_requirements !"" phpunit.xml.dist !"" src !"" var # !"" bootstrap.php.cache # !"" cache # !"" logs # $"" SymfonyRequirements.php $"" web

Slide 13

Slide 13 text

Project Structure !"" SomeNamespace # !"" Bundles # !"" Themes # $"" Utilities !"" CompanyName # !"" CampaignBundle # $"" RedactorBundle !"" Vagrant !"" build.xml !"" default.build.properties $"" symfony !"" app !"" composer.json !"" schema.xml $"" web

Slide 14

Slide 14 text

Project Structure • http://symfony.com/doc/current/cookbook/ configuration/override_dir_structure.html • http://rad.knplabs.com/ • https://github.com/symfony/symfony- standard/issues/ 584#issuecomment-23148928

Slide 15

Slide 15 text

Environments http://www.flickr.com/photos/minustah/5809683503/

Slide 16

Slide 16 text

Environments • dev • prod • test

Slide 17

Slide 17 text

Environments !"" app # !"" config # # !"" config.yml # # !"" config_alex.yml # # !"" config_artur.yml # # !"" config_dev.yml # # !"" config_john.yml # # !"" config_prod.yml # # !"" config_test.yml # # !"" parameters.yml # # !"" ... $"" web !"" app.php !"" app_alex.php !"" app_artur.php !"" app_dev.php !"" app_john.php

Slide 18

Slide 18 text

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

Slide 29

Slide 29 text

Composer

Slide 30

Slide 30 text

Composer. The bad way.

Slide 31

Slide 31 text

Composer

Slide 32

Slide 32 text

Annoying Composer? {"scripts": {"post-install-cmd": "rm -rf /"}}

Slide 33

Slide 33 text

Annoying Composer? {"scripts": {"post-install-cmd": “apt-get install ruby“}}

Slide 34

Slide 34 text

Annoying Composer? {"scripts": {"post-install-cmd": “apt-get uninstall php“}}

Slide 35

Slide 35 text

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/

Slide 47

Slide 47 text

Composer "minimum-stability": "dev", "prefer-stable": false,

Slide 48

Slide 48 text

Git

Slide 49

Slide 49 text

.gitignore *.iml *.DS_Store .idea Acme/DemoBundle/AcmeDemoBundle.php Acme/DemoBundle/Controller/DemoController.php Acme/DemoBundle/Controller/SecuredController.php Acme/DemoBundle/Controller/WelcomeController.php Acme/DemoBundle/DependencyInjection/AcmeDemoExtension.php Acme/DemoBundle/EventListener/ControllerListener.php Acme/DemoBundle/Form/ContactType.php Acme/DemoBundle/Resources/config/services.xml Acme/DemoBundle/Resources/public/css/demo.css Acme/DemoBundle/Resources/public/images/blue-arrow.png Acme/DemoBundle/Resources/public/images/field-background.gif Acme/DemoBundle/Resources/public/images/logo.gif Acme/DemoBundle/Resources/public/images/search.png Acme/DemoBundle/Twig/Extension/DemoExtension.php ...

Slide 50

Slide 50 text

.gitignore /web/bundles/ /app/bootstrap* /app/cache/* /app/logs/* /vendor/ /app/config/parameters.yml

Slide 51

Slide 51 text

Wut .gitignore?! • vendors • compiled assets • Acme • parameters.yml • composer.lock

Slide 52

Slide 52 text

Wut .gitignore?! https://help.github.com/articles/ignoring-files

Slide 53

Slide 53 text

DI http://www.flickr.com/photos/blueship/137771066/

Slide 54

Slide 54 text

Classic some_name.cluster_access.switcher.twig.extension: class: App\Bundles\SomeBundle\Twig\SomeExtension arguments: [@service_container] tags: - { name: twig.extension }

Slide 55

Slide 55 text

Classic class SomeExtension extends \Twig_Extension { ... $cluster = $this->container ->get('some.manager') ->getCurrentSomething(); ... }

Slide 56

Slide 56 text

Extended app.rubric.service: class: App\DefaultBundle\Service\RubricService arguments: [@security.context, ...]

Slide 57

Slide 57 text

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 ); }

Slide 77

Slide 77 text

Be polite bin/php-cs-fixer fix src

Slide 78

Slide 78 text

You probably heard • Fat controllers • Tight coupling • Code immobility

Slide 79

Slide 79 text

Controller on a diet • get Request • init service • call service’s method with params • return rendered Response

Slide 80

Slide 80 text

README

Slide 81

Slide 81 text

Don’t hurt your self and be cool.

Slide 82

Slide 82 text

Final bullet

Slide 83

Slide 83 text

Thank you! Roma Lapin Evercode Lab [email protected] @memphys @evercodelab