typo3 flow package best practices

83
Package best practices April 20, 2013 Saturday 20 April 13 / CW:

Upload: pankaj-lele

Post on 01-Nov-2014

2.913 views

Category:

Technology


2 download

DESCRIPTION

Talk at Inspiring Flow 2013

TRANSCRIPT

Page 1: TYPO3 Flow package best practices

Package best practices

April 20, 2013

Saturday 20 April 13 / CW:

Page 2: TYPO3 Flow package best practices

Flow package best practices

Pankaj Lele - [email protected]

April 20, 2013

About me

• Pankaj Lele

• Live in Goa, India

• Lelesys Founder Director and CTO (10+ years)

• @pankajlele

2

Saturday 20 April 13 / CW:

Page 3: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

My Flow experience

3

• Large scale enterprise project

• Full agile development with 7 teams, 3 countries, 20+ developers

• Start with FLOW3 1.0 Nov. 2011

• First public version March 2012

Saturday 20 April 13 / CW:

Page 4: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

45 best practices for ...

4

• Efficiency

• Assuring quality

• Avoiding common errors

• Flexibility

Saturday 20 April 13 / CW:

Page 5: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

5

Best practices

Architecture Coding

Saturday 20 April 13 / CW:

Page 6: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

6

A deep thinking architect ...

Saturday 20 April 13 / CW:

Page 7: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#1

• Good to define full class design with UML

7

Architecture

Saturday 20 April 13 / CW:

Page 8: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#1

8

Architecture

ArgoUML

(all platforms)

Saturday 20 April 13 / CW:

Page 9: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#2

• Avoid unnecessary bi-directional associations in your domain model

9

Architecture

Saturday 20 April 13 / CW:

Page 10: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#3

• Always good to define interfaces

• defaults - Objects.yaml

• ReflectionService

10

Architecture

Saturday 20 April 13 / CW:

Page 11: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#3

11

Architecture

For example, PaymentHandlerInterface

Saturday 20 April 13 / CW:

Page 12: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#4

• Avoid defining static functions

• PhpUnit limitations for mocks

12

Architecture

Saturday 20 April 13 / CW:

Page 13: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#5

• Good to define domain services

13

Architecture

Saturday 20 April 13 / CW:

Page 14: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#5

14

Architecture

Saturday 20 April 13 / CW:

Page 15: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#6

• Good to use doctrine inheritance mapping if possible

15

Architecture

Saturday 20 April 13 / CW:

Page 16: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#6

16

Architecture

BAD

class Media { /** * The title of media * @var string */ protected $title; /** * The type one of 'audio' or 'video' * @var string */ protected $type; }

Saturday 20 April 13 / CW:

Page 17: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#6

16

Architecture

BAD

class Media { /** * The title of media * @var string */ protected $title; /** * The type one of 'audio' or 'video' * @var string */ protected $type; }

Saturday 20 April 13 / CW:

Page 18: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#6

17

Architecture

GOOD

class AbstractMedia { /** * The title of media * @var string */ protected $title; }

class Audio extends AbstractMedia { }

class Video extends AbstractMedia { }

Saturday 20 April 13 / CW:

Page 19: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#6

17

Architecture

GOOD

class AbstractMedia { /** * The title of media * @var string */ protected $title; }

class Audio extends AbstractMedia { }

class Video extends AbstractMedia { }

Saturday 20 April 13 / CW:

Page 20: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#7

• Good to define class constants

• no need to remember values

18

Architecture

Saturday 20 April 13 / CW:

Page 21: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#7

19

Architecture

GOOD

class ElectronicAddress { const TYPE_AIM = 'Aim'; const TYPE_EMAIL = 'Email'; const TYPE_ICQ = 'Icq'; const TYPE_JABBER = 'Jabber'; }

$electronicAddress = new ElectronicAddress(); $electronicAddress->setType(ElectronicAddress::TYPE_EMAIL);

Saturday 20 April 13 / CW:

Page 22: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#7

19

Architecture

GOOD

class ElectronicAddress { const TYPE_AIM = 'Aim'; const TYPE_EMAIL = 'Email'; const TYPE_ICQ = 'Icq'; const TYPE_JABBER = 'Jabber'; }

$electronicAddress = new ElectronicAddress(); $electronicAddress->setType(ElectronicAddress::TYPE_EMAIL);

Saturday 20 April 13 / CW:

Page 23: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

20

Best practices

Architecture Coding

Saturday 20 April 13 / CW:

Page 24: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Post-kickstart

21

Saturday 20 April 13 / CW:

Page 25: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#8

• Good to define methods addX(), removeX() and getXs() for collections

• example: TYPO3\Party\Domain\Model\AbstractParty

22

Coding:Post-kickstart

Saturday 20 April 13 / CW:

Page 26: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#9

• Always good to initialize collections in entity class constructor

• recommended by Doctrine

23

Coding:Post-kickstart

Saturday 20 April 13 / CW:

Page 27: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#10

• Always annotate properties with validators right away after kickstart

24

Coding:Post-kickstart

Saturday 20 April 13 / CW:

Page 28: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#11

• Always remove unused controller actions created by kick-starter

25

Coding:Post-kickstart

Saturday 20 April 13 / CW:

Page 29: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Debugging

26

Saturday 20 April 13 / CW:

Page 30: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#12

• Always use \TYPO3\Flow\var_dump() for debugging variables

• no more memory full errors

• object information

27

Coding:Debugging

Saturday 20 April 13 / CW:

Page 31: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#12

28

Coding:Debugging

Saturday 20 April 13 / CW:

Page 32: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#13

• Best to use package debug/toolbar

• queries executed

• functions called

29

Coding:Debugging

Saturday 20 April 13 / CW:

Page 33: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Composer

30

Saturday 20 April 13 / CW:

Page 34: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#14

• Always maintain composer.json right away

• document what you used

• ext-zip or ext-imagick

31

Coding:Composer

Saturday 20 April 13 / CW:

Page 35: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Configuration

32

Saturday 20 April 13 / CW:

Page 36: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#15

• Good to set defaultLocale setting

• application’s default language

• helps formatting numbers and currencies automatically

33

Coding:Configuration

Saturday 20 April 13 / CW:

Page 37: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#16

• Always run ./flow configuration:validate to check whether package *.yaml files are valid

34

Coding:Configuration

Saturday 20 April 13 / CW:

Page 38: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#17

• Always crosscheck package configuration using ./flow configuration:show

35

Coding:Configuration

Saturday 20 April 13 / CW:

Page 39: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

MVC

36

Saturday 20 April 13 / CW:

Page 40: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#18

• Always map all GET/POST variables as action arguments

37

Coding:MVC

Saturday 20 April 13 / CW:

Page 41: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#18

38

Coding:MVC

/** * Create action * * @return void */ public function createAction() { $newVideo = $this->request->getHttpRequest()->getArgument('newVideo'); // .... }

BAD

Saturday 20 April 13 / CW:

Page 42: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#18

39

Coding:MVC

/** * Create action * * @param Video $newVideo The video * @return void */ public function createAction(Video $newVideo) { // .... }

Good

Saturday 20 April 13 / CW:

Page 43: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#19

• Good to have an AbstractController

• assign logged in user to the view

• inject common dependencies

40

Coding:MVC

Saturday 20 April 13 / CW:

Page 44: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#20

• Avoid exit() call inside controller action

• StopActionException() is what you want

41

Coding:MVC

Saturday 20 April 13 / CW:

Page 45: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#21

• Good to use StandAloneView

• render e-mail templates

42

Coding:MVC

Saturday 20 April 13 / CW:

Page 46: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Session

43

Saturday 20 April 13 / CW:

Page 47: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#22

• Avoid using direct session API

• encapsulate

• session scoped class

• example: ShoppingCart

44

Coding:Session

Saturday 20 April 13 / CW:

Page 48: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Dependency Injection

45

Saturday 20 April 13 / CW:

Page 49: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#23

• Always inject interfaces

• proper factory object is used

• change implementation via Objects.yaml

46

Coding:Dependency

Injection

Saturday 20 April 13 / CW:

Page 50: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#24

• inject TYPO3\Flow\Session\SessionInterface

• current active session

• inject TYPO3\Flow\Session\Session

• completely new session

47

Coding:Dependency

InjectionIf you really want to ...

Saturday 20 April 13 / CW:

Page 51: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Persistence

48

Saturday 20 April 13 / CW:

Page 52: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#25

• Frequently call ./flow doctrine:validate

• after kickstart + modifications

• after each change in domain model

49

Coding:Persistence

Saturday 20 April 13 / CW:

Page 53: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#26

• Always use doctrine migrations for database update

• doctrine:update/create not good for serious projects

50

Coding:Persistence

Saturday 20 April 13 / CW:

Page 54: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#27

• Always test “down” doctrine migrations

• good for change rollback

51

Coding:Persistence

Saturday 20 April 13 / CW:

Page 55: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#28

• Always separate out doctrine migrations of respective packages

52

Coding:Persistence

Saturday 20 April 13 / CW:

Page 56: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#29

• Good to use doctrine migrations also for migrating data

• $this->connection

53

Coding:Persistence

Saturday 20 April 13 / CW:

Page 57: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#30

• Good do not mention any collation or use utf8_unicode_ci in doctrine migrations

• my.cnf "collation-server"

54

Coding:Persistence

Saturday 20 April 13 / CW:

Page 58: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#31

• Do not delete/modify doctrine migration files once committed

• always generate new migration on each domain model change

55

Coding:Persistence

Saturday 20 April 13 / CW:

Page 59: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#32

• Always pull upstream code changes

• doctrine:migrate

• doctrine:migrationgenerate

56

Coding:Persistence

Saturday 20 April 13 / CW:

Page 60: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#33

• Good to use DQL at some places

• aggregations

• joins

• avoid nested foreach loops

• warning: QOM based widgets

57

Coding:Persistence

Saturday 20 April 13 / CW:

Page 61: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Resources

58

Saturday 20 April 13 / CW:

Page 62: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#34

• If needed, good to write files in /Data

• Persistent

• Temporary

• Utility\Environment

59

Coding:Resources

Saturday 20 April 13 / CW:

Page 63: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Development Environment

60

Saturday 20 April 13 / CW:

Page 64: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#35

• Good to ignore directories in your favorite IDE

• Data/Temporary, Logs, Persistent

• speeds up the IDE

• proxy classes ignored

61

Coding:Development Environment

Saturday 20 April 13 / CW:

Page 65: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#36

• Good to freeze packages not changed so often

• freeze framework packages

• ./flow package:freeze

62

Coding:Development Environment

Saturday 20 April 13 / CW:

Page 66: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Error Proofing

63

Saturday 20 April 13 / CW:

Page 67: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#37

• Always type-hint method arguments

• strict expectations

64

Coding:Error proofing

Saturday 20 April 13 / CW:

Page 68: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Error Handling

65

Saturday 20 April 13 / CW:

Page 69: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#38

• Always throw exceptions from domain services

• catch in controllers

• better error reporting to the user

66

Coding:Error handling

Saturday 20 April 13 / CW:

Page 70: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Utilities

67

Saturday 20 April 13 / CW:

Page 71: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#39

• Always look for available TYPO3\Flow\Utility

• get things done the way framework likes

• environment, files, algorithms, array

68

Coding:Utilities

Saturday 20 April 13 / CW:

Page 72: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Bootstrap

69

Saturday 20 April 13 / CW:

Page 73: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#40

• Good to rely on FLOW_* constants

• paths

70

Coding:Bootstrap

Saturday 20 April 13 / CW:

Page 74: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Troubleshooting

71

Saturday 20 April 13 / CW:

Page 75: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#41

• Always find exact database error

• SHOW INNODB STATUS

72

Coding:Troubleshooting

Saturday 20 April 13 / CW:

Page 76: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Testing

73

Saturday 20 April 13 / CW:

Page 77: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#42

• Unit tests which test business logic

• usually test domain services

• persistence mocked

74

Coding:Testing

Saturday 20 April 13 / CW:

Page 78: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#43

• Functional tests which test user stories

• test controllers

• virtual browser

• in-memory persistence

75

Coding:Testing

Saturday 20 April 13 / CW:

Page 79: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Performance

76

Saturday 20 April 13 / CW:

Page 80: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#44

• Good to analyze performance

• sandstorm/plumber

• xhprof just profiler

• inclusive/exclusive timing

• no code changes

77

Coding:Performance

Saturday 20 April 13 / CW:

Page 81: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

#45

• Always good to cache stuff

• caching framework

• front-ends

• backends

78

Coding:Performance

Saturday 20 April 13 / CW:

Page 82: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Thank you!

79

Saturday 20 April 13 / CW:

Page 83: TYPO3 Flow package best practices

Pankaj Lele - [email protected]

Flow package best practicesApril 20, 2013

Questions?

80

Saturday 20 April 13 / CW: