modernize your drupal development

109
Modernize Your Drupal Development Chris Tankersley php[world] 2014

Upload: chris-tankersley

Post on 25-Jun-2015

273 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Modernize Your Drupal Development

Modernize Your Drupal Development

Chris Tankersley

php[world] 2014

Page 2: Modernize Your Drupal Development

2

Who Am I?

● A PHP Developer for 10 Years● Drupal Dev for 4 years● Lots of projects no one uses, and a few

some do● https://github.com/dragonmantank

Page 3: Modernize Your Drupal Development

3

We've got a lot to cover

● Development Environments● Version Control● drush● Coding Standards● Building Better Modules● Moving Out of the Database● Putting it all together

Page 4: Modernize Your Drupal Development

4

Development Environments

Page 5: Modernize Your Drupal Development

5

Local Stack

● Run everything off of your local machine!

● Most performant of the options

Page 6: Modernize Your Drupal Development

6

Local Stack

● Not very portable● More junk on your PC to maintain● Probably not at all like Production

Page 7: Modernize Your Drupal Development

7

Don't use Portable *AMP stacks

Page 8: Modernize Your Drupal Development

8

If you have to...

● Acquia Dev Desktop● Zend Server

Page 9: Modernize Your Drupal Development

9

Acquia Dev Desktop

● Pre-built *AMP Stack● Available for Drupal 6 or 7● Install and Ready to Go

Page 10: Modernize Your Drupal Development

10

Acquia Dev Desktop

● Not built for multiple installs● Can’t use for existing sites● Only for Windows and Mac

Page 11: Modernize Your Drupal Development

11

Zend Server

● Uses OS's server● Has some cool tools like Z-Ray● Mostly a wrapper around the local stack● Support!● Has a Development and Production

version

Page 12: Modernize Your Drupal Development

12

Zend Server

● Weird issues with permissions● Works best with other Zend tools● Pricey

Page 13: Modernize Your Drupal Development

13

How it looks

Your OS

Applications

Your App

Web Server DB Server

Page 14: Modernize Your Drupal Development

14

docker

● Containers for applications● Makes deployment very easy● Very easy to get set up● Way more performant than VMs

Page 15: Modernize Your Drupal Development

15

docker

● If you're not on Linux, think really hard about using

● Mostly a deployment fixer

Page 16: Modernize Your Drupal Development

16

How it looks - Linux

Your OS

Applications

Your App

Web Server DB Server

Page 17: Modernize Your Drupal Development

17

How it looks – Everywhere Else

Your OS

Applications Virtualization Layer

Virtualized OS

Your App

Web Server DB Server

Page 18: Modernize Your Drupal Development

18

vagrant

● Full server to run code● Self contained and can be replicated● Most modern machines can do VM

Page 19: Modernize Your Drupal Development

19

vagrant

● Uses more resources● Easier to break● When it breaks, it breaks hard

Page 20: Modernize Your Drupal Development

20

How it looks

Your OS

Applications Virtualization Layer

Virtualized OS

Your App

Web Server DB Server

Page 21: Modernize Your Drupal Development

21

A quick demo

Page 22: Modernize Your Drupal Development

22

Considerations

● How special is my Production environment?● What resources do my dev machines have?● How many things am I working on?● What's the tech level of my coworkers?● What's the tech level of my clients?

Page 23: Modernize Your Drupal Development

23

Version Control

Page 24: Modernize Your Drupal Development

24

What is version control?

● Some system that keeps track of changes to source code

Page 25: Modernize Your Drupal Development

25

Many different systems

● Git● Subversions● Mercurial

Page 26: Modernize Your Drupal Development

26

It doesn't matter which one you use

Page 27: Modernize Your Drupal Development

27

A Quick Demo

Page 28: Modernize Your Drupal Development

28

Workflows

● Github/Pull Request● Gitflow● Rebase

Page 29: Modernize Your Drupal Development

29

GitHub Workflow

Page 30: Modernize Your Drupal Development

30

For more info...

https://guides.github.com/introduction/flow/index.html

Page 31: Modernize Your Drupal Development

31

Rebase Workflow

Page 32: Modernize Your Drupal Development

32

For more info...

http://randyfay.com/content/rebase-workflow-git

Page 33: Modernize Your Drupal Development

33

gitflow

Page 34: Modernize Your Drupal Development

34

masterdevelop

Tag: 0.9.2

v0.9blog_rework staff_page

Tag: 1.0.0

v1.0

Tag: 0.9.3

Page 35: Modernize Your Drupal Development

35

For more info...

http://nvie.com/posts/a-successful-git-branching-model/

Page 36: Modernize Your Drupal Development

36

Jeff Carouth's „Git and Github – Working Effectively on a Team“

https://speakerdeck.com/jcarouth/git-and-github-working-effectively-on-a-team-at-php-

tek-2014

Page 37: Modernize Your Drupal Development

37

Any questions?

Page 38: Modernize Your Drupal Development

38

Are we finally ready for some actual Drupal?

Page 39: Modernize Your Drupal Development

39

Page 40: Modernize Your Drupal Development

40

drush

Page 41: Modernize Your Drupal Development

41

What is drush?

Page 42: Modernize Your Drupal Development

42

What is drush?

● Command line interface for (much of) Drupal

● Allows modules to be CLI driven● Much, much quicker than the GUI

Page 43: Modernize Your Drupal Development

43

How does it work?

Page 44: Modernize Your Drupal Development

44

Common Commands

Page 45: Modernize Your Drupal Development

45

Download Drupal

$ drush dl drupal --drupal-project-rename="drupal"Project drupal (7.32) downloaded to /vagrant/drupal-7.32. [success]Project drupal contains: [success] - 3 profiles: testing, standard, minimal - 4 themes: stark, seven, garland, bartik - 47 modules: drupal_system_listing_incompatible_test, drupal_system_listing_compatible_test, user, update, trigger, translation, tracker, toolbar, taxonomy, system, syslog, statistics, simpletest, shortcut, search, rdf, profile, poll, php, path,overlay, openid, node, menu, locale, image, help, forum, filter, file, field_ui, text, options, number, list, field_sql_storage, field, dblog, dashboard, contextual, contact, comment, color, book, blog, block, aggregator

Page 46: Modernize Your Drupal Development

46

Install Drupal

$ drush site-install standard -y --db-url=mysql://drupal:DrupalR0cks@localhost/drupaldb --account-name=admin --account-pass=admin --site-name="My Drupal Site" [email protected]

Page 47: Modernize Your Drupal Development

47

Run a development server

$ drush runserver 8080

$ drush runserver 0.0.0.0:8080

Page 48: Modernize Your Drupal Development

48

Watching the Watchdog

// Show the last 10 messages

$ drush watchdog-show

// Show the last 50 messages

$ drush watchdog-show --count=50

// Show only entries of a specific severity

$ drush watchdog-show --severity=notice

// Search for a specific message

$ drush watchdog-show "cron run successful"

Page 49: Modernize Your Drupal Development

49

Viewing Watchdog Entries

$ drush watchdog-show 54

ID : 54

Date : 30/Jan 22:10

Type : system

Severity : info

Message : overlay module disabled.

Page 50: Modernize Your Drupal Development

50

Cleaning Up After the (Watch)dog

// Destroy it all!

$ drush watchdog-delete all

// Delete a specific one to hide an error you generated that no one can know about

$ drush watchdog-delete 50

// Delete all messages containing a string

$ drush watchdog-delete "cron run successfull"

// Delete everything of a specific severity

$ drush watchdog-delete --severity=debug

Page 51: Modernize Your Drupal Development

51

Working with modules

$ drush pm-download backup_migrate

$ drush pm-enable backup_migrate

$ drush pm-update

Page 52: Modernize Your Drupal Development

52

drush Aliases

● Allows you to tell drush where an external site is located at

● Requires drush on the other end, and shell access

Page 53: Modernize Your Drupal Development

53

Creating an alias

// sites/all/drush/prod.alias.drushrc.php

$aliases['prod'] = array(

'uri' => 'mysite.com',

'root' => '/public_html/',

'remote-host' => '10.0.2.2',

'remote-user' => 'mysite',

);

Page 54: Modernize Your Drupal Development

54

Using an alias

$ drush @prod status

PHP configuration : /Applications/acquia-drupal/php5_3/bin/php.ini

Drush version : 5.7

Drush configuration :

Page 55: Modernize Your Drupal Development

55

Common uses for aliases

$ drush rsync sites/default/files/ @prod:sites/default/files

$ drush sql-sync @prod @self

$ drush @prod site-install standard [...]

Page 56: Modernize Your Drupal Development

56

Deploying code with drush

$ drush rsync @self @prod

You will destroy data from [email protected]:'~/Sites/deploy/' and replace with data from /vagrant/

Do you really want to continue? (y/n): y

Page 57: Modernize Your Drupal Development

57

Questions?

Page 58: Modernize Your Drupal Development

58

Coding Standards

Page 59: Modernize Your Drupal Development

59

Huh?

Coding standards are a list of rules regarding the layout and structure of source code.

Page 60: Modernize Your Drupal Development

60

What?

● Two space indents● Spaces between casts

– $id = (int) $_POST['id'];

● Not using else if, using elseif● Always using curly braces on control

structures

Page 61: Modernize Your Drupal Development

61

Why?

● Makes it easy to read code● Makes it easy to merge code● You're on a team, act like it● If you want your code on drupal.org, you're

going to need to follow it● If you want to contribute, you'll need to follow it

Page 62: Modernize Your Drupal Development

62

For the nitty-gritty...

● https://www.drupal.org/coding-standards

Page 63: Modernize Your Drupal Development

63

Tools

● Code Sniffer with Drupal rules● Coder● PAReview● Grammer Parser

Page 64: Modernize Your Drupal Development

64

Code Sniffer

● Compares your code to some coding standard

● General PHP tool, not specific to Drupal● CLI

Page 65: Modernize Your Drupal Development

65

Code Sniffer

https://www.drupal.org/node/1419988

Page 66: Modernize Your Drupal Development

66

Demo Time

Page 67: Modernize Your Drupal Development

67

Coder

● GUI to check your code against coding standards

● https://www.drupal.org/project/coder

Page 68: Modernize Your Drupal Development

68

Demo Time

Page 69: Modernize Your Drupal Development

69

PAReview

● Project Application Review● First line of defense against bad

modules● Online code sniffer for drupal.org

Page 70: Modernize Your Drupal Development

70

Self Hosted

● Add the Drupal, DrupalSecure, DrupalPractice, and Codespell standards to CodeSniffer

● Download the bash script from Github

Page 71: Modernize Your Drupal Development

71

Install DrupalPractice

$ git clone https://github.com/klausi/drupalpractice.git ~/.drush/drupalpractice

$ ln -sv ~/.drush/drupalpractice/DrupalPractice ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/

Page 72: Modernize Your Drupal Development

72

Quick Demo

Page 73: Modernize Your Drupal Development

73

Questions?

Page 74: Modernize Your Drupal Development

74

Building Better Modules

Page 75: Modernize Your Drupal Development

75

Drupal supplies a lot of things for you

● Form API● Schema API● Theme Layer● Unit Testing● Entities

Page 76: Modernize Your Drupal Development

76

Unit Testing

Page 77: Modernize Your Drupal Development

77

How Test Driven Development works

● Write your tests before your code● Watch it fail● Write code to make your test pass● Feel better! (and refactor)

Page 78: Modernize Your Drupal Development

78

TDD In Drupal

● Drupal ships with SimpleTest baked in● Supports unit testing and functional testing● Unit tests are done by extending DrupalUnitTestCase

● Functional tests are done by extending DrupalWebTestCase

Page 79: Modernize Your Drupal Development

79

Unit Tests vs Functional Tests

● Unit tests should focus on testing an individual piece of code

● Functional tests should focus on testing output/pages

Page 80: Modernize Your Drupal Development

80

Unit Tests vs Functional Tests

● Unit tests do not bootstrap Drupal, so are very quick

● Functional tests do bootstrap Drupal, so are very slow

Page 81: Modernize Your Drupal Development

81

Downsides to TDD in Drupal

● The GUI is an AJAX runner, which breaks a lot– Use drush for a better experience

● Debugging can be very hard, since the environment is created and destroyed each test– Use $this->verbose() or debug()

Page 82: Modernize Your Drupal Development

82

Let's build a test

Page 83: Modernize Your Drupal Development

83

Let's build a test

Page 84: Modernize Your Drupal Development

84

Caching and Asset Aggregation

● Drupal has a caching layer● Drupal has a basic asset pipeline

Page 85: Modernize Your Drupal Development

85

Turn it on on Performance

Page 86: Modernize Your Drupal Development

86

Turn it on on Performance

Page 87: Modernize Your Drupal Development

87

Caching

● Caching saves a chunk of the render array to the DB

● Caching still requires a DB hit

Page 88: Modernize Your Drupal Development

88

Two Different Caches

● Page caching for full output● Block caching for dynamic content

Page 89: Modernize Your Drupal Development

89

Asset Aggregation

● Groups CSS and JS together, reducing HTTP calls

● Will minify the CSS, reducing the transmission size

Page 90: Modernize Your Drupal Development

90

Easy to take advantage of

● Let Drupal know about your files– drupal_add_js()– drupal_add_css()– Through #attached– Add it to your .info file

● Don't just add straight to templates

Page 91: Modernize Your Drupal Development

91

Adding JS

Page 92: Modernize Your Drupal Development

92

Using #attached

Page 93: Modernize Your Drupal Development

93

Entities

● Basic building blocks of 'things' in Drupal

● You already use them● Entity API provides an interface for

making your own

Page 94: Modernize Your Drupal Development

94

Why use them?

● Drupal takes care of a lot of scaffolding● Allows site builders to extend them

Page 95: Modernize Your Drupal Development

95

What is an entity?

● A [Drupalized] thing● Series of classes and functions that tell

Drupal how to deal with your thing

Page 96: Modernize Your Drupal Development

96

What do we need?

● A dependency on the Entities module

Page 97: Modernize Your Drupal Development

97

A place to store things...

Page 98: Modernize Your Drupal Development

98

And now we create a class for our entity

Page 99: Modernize Your Drupal Development

99

And now we tell Drupal about it

Page 100: Modernize Your Drupal Development

100

Let's create an admin form

Page 101: Modernize Your Drupal Development

101

Now let's use it!

Page 102: Modernize Your Drupal Development

102

Moving Out of the Database

Page 103: Modernize Your Drupal Development

103

The Database Sucks

● Drupal stores a lot of things in the database

● Databases are hard to version● Database info is hard to move

Page 104: Modernize Your Drupal Development

104

Features!

● Features allow you to package up stuff into modules

● Makes deploying code much easier

Page 105: Modernize Your Drupal Development

105

Bundle things together

Page 106: Modernize Your Drupal Development

106

Bundle lots of things

Page 107: Modernize Your Drupal Development

107

Page 108: Modernize Your Drupal Development

108

Questions?

Page 109: Modernize Your Drupal Development

109

Thanks!

● http://joind.in/talk/view/11901● @dragonmantank● [email protected]