37
© 2016 Magento, Inc. Page | 1 Tips and Best Practices for Developing on Magento 2 Magento Meetup Dublin - October 26 2016

Tips and Best Practices for Developing on Magento 2

Embed Size (px)

Citation preview

© 2016 Magento, Inc. Page | 1

Tips and Best Practices for Developing on Magento 2 Magento Meetup Dublin - October 26, 2016

© 2016 Magento, Inc. Page | 2

Solutions ArchitectMagento Expert Consulting GroupTwitter: @gordonknoppe

Gordon Knoppe

© 2016 Magento, Inc. Page | 3

Based on what I am seeing today

© 2016 Magento, Inc. Page | 4

You may already be doing some of this

© 2016 Magento, Inc. Page | 5

Not many teams are doing all…

© 2016 Magento, Inc. Page | 6

Each slide could be it’s own talk...

© 2016 Magento, Inc. Page | 7

But not as important as that three hours you spent looking at dark themes for your IDE…

© 2016 Magento, Inc. Page | 8

Quick show of hands

© 2016 Magento, Inc. Page | 9

Magento source, project structure

composer create-project --repository-url=https://repo.magento.com/ \ magento/project-community-edition .

http://devdocs.magento.com/guides/v2.0/install-gde/prereq/integrator_install.html

© 2016 Magento, Inc. Page | 10

Magento source, gitignore

/app/code/Magento/app/design/*/Magento/app/etc/*.*/app/etc/enterprise/app/i18n/magento/app/*.*/bin/dev/lib/node_modules/phpserver

/pub/setup/update/var/vendor/*.*!/app/etc/config.php!/.gitignore!/composer.json!/composer.lock

https://github.com/gordonknoppe/m2-sample-project

© 2016 Magento, Inc. Page | 11

Magento source, composer for dependencies

https://getcomposer.org M1 composer: http://fbrnc.net/blog/2014/12/magento-update

© 2016 Magento, Inc. Page | 12

Magento source, composer for dependencies

Paid extension or no composer.json? Make your own repo and add composer.json

© 2016 Magento, Inc. Page | 13

Magento source, composer for dependencies

require-dev for developer dependencies (debug toolkit maybe?)

http://store.pulsestorm.net/products/commerce-bug-3

© 2016 Magento, Inc. Page | 14

Magento source, patches are still a reality

• Create a patches/ directory to drop patch files into• Write a simple bash script which loops and applies on build• See patches and re-order patches is trivial• 01, 02, 03, etc.

© 2016 Magento, Inc. Page | 15

Magento source, simple reset/build script

• Cleanup (destructive!)– git clean -ffxd src/

• Compose– composer install

• Patch– find patches -type f -name '*.sh' | while read file

do # copy patch into src and apply...done

• Seems scriptable right? (reset.sh, stash-reset.sh?)

© 2016 Magento, Inc. Page | 16

Features and bug fixes

• Developing in branches

• Different styles during lifecycle of project

– Sprint on master during project build phase

– Switch to branches for bug fixes and maintenance

– Bugfix branch based on latest stable (master)

• Means you can stage, test, and deploy combination of features/fixes

• Don’t let a single bug block an entire maintenance release

• git-release to pick branches and build RCs

https://github.com/gdoug-guidance/git-release

© 2016 Magento, Inc. Page | 17

PHPStorm - IDE

• Inspections and validation

• Code navigation

• Automatic formatting

• Autocomplete and parameter hinting

• XSD based XML validation

• Quick survey?

© 2016 Magento, Inc. Page | 18

PHPStorm - IDE

https://github.com/magento-ecg/coding-standard

© 2016 Magento, Inc. Page | 19

PHPStorm - New > File templates

https://gist.github.com/Vinai/eb4713bda65fb8b467b1

© 2016 Magento, Inc. Page | 20

PHPStorm - Local File History

© 2016 Magento, Inc. Page | 21

PHPStorm - Magento 2 plugin

• Configuration smart completion and references

– di.xml

– layouts

– events.xml

– webapi.xml

• Inspections

– @api usage

– ObjectManager usage

• Code helpers

– "Navigate to configuration" reference in scope of class/interface

– "Go to plugin" reference in scope of class/interface and method

– "Navigate to Web API configuration" reference in scope of class/interface and method

https://plugins.jetbrains.com/plugin/8024

© 2016 Magento, Inc. Page | 22

PHPStorm - “Mark directory as..”

– /bin

– /dev

– /pub

– /setup

– /var/cache

– /var/log

– /var/page_cache

– var/view_processed

– vendor/magento/magento2-

base

https://plugins.jetbrains.com/plugin/8024

Recommended directories to exclude

© 2016 Magento, Inc. Page | 23

PHPStorm - Test Driven Development

© 2016 Magento, Inc. Page | 24

PHPStorm - Test Driven Development

http://vinaikopp.com/2016/02/05/01_the_skeleton_module_kata/

© 2016 Magento, Inc. Page | 25

PHPStorm - Test Driven Development

• Testing during active development– Configure a custom test suite for just your code base

• Speed up integration test configuration (but understand why)– <const name="TESTS_CLEANUP" value="disabled"/>– Database is not reinstalled on each test run

© 2016 Magento, Inc. Page | 26

Development - Data issues

• Sample data

• Database snapshots, script a refresh

– Filesystem snapshots (VirtualBox, etc.)

– Fake a MySQL snapshot by importing switching databases in config

• Continual testing of setup:install, not just setup:upgrade

© 2016 Magento, Inc. Page | 27

Development - Error logs and messages

• Tail error logs during dev in the PHPStorm terminal– tail -f var/log/*.log

• A clean log is easier to tail when debugging your future self will thank you– Fix trivial NOTICE and WARNING messages

• Production logging– Application clusters need a centralized logging solution

• Logstash• Loggly• And many more...

© 2016 Magento, Inc. Page | 28

Deployment

• Yes, you need an actual process

• One-click or no-click deploys should be the first step of your project

• Dev, stage, and prod should all use it identically (parameterize)

• Zero downtime deployments (assuming no schema updates)

© 2016 Magento, Inc. Page | 29

Deployment

• With Magento 2 that means a build process

– Ship artifacts, don’t build on application servers

– Can ship using tar/zip files but also another git repo

• Visibility into exact file changes between deploys (including upgrades and

dependencies)

• Atomic deployments

– A/B vs. Timestamped deployments and the thundering herd

• Two directories with a symlink switch (careful with document root setting)

• Two git clones (if shipping built code via repo)

• Preserve your opcode cachehttps://youtu.be/MT4rRWKygq0?t=46m18s - Rasmus Lerdorf on PHP 7 (Skip to 46 minute for deployment topic)

© 2016 Magento, Inc. Page | 30

Performance and optimization

• Performance optimization shouldn’t be left for week before launch

• Tools like New Relic and Blackfire give real world insights

• During development

– Block cache and page cache off to test your improvements

• Remove unused or dead code

• Disable core functionality which you don’t use

– Observers or even whole extensions (sometimes)

© 2016 Magento, Inc. Page | 31

Performance and optimization

• Develop and test using sample data (all product types, etc.)

• Fixtures from Magento performance tests provide more product data

• Add on with your own sample data package

http://devdocs.magento.com/guides/v2.1/config-guide/cli/config-cli-subcommands-perf-data.html

© 2016 Magento, Inc. Page | 32

Performance and optimization

siege -c 1 -r 10 http://www.mysite.docker/

parse = false # in ~/.siege/siege.conf(for isolating app performance)

© 2016 Magento, Inc. Page | 33

Local Development with Docker

• https://github.com/gordonknoppe/docker-magento2-example• Composed docker images not monolith

– Easy swap for testing PHP/MySQL versions• 5.6 to 7.0• MySQL / Percona / MariaDB

• Docker images– https://hub.docker.com/u/gknoppe/

© 2016 Magento, Inc. Page | 34

OS X Development with Docker - Brew

• Install brew (http://brew.sh) • Install dependencies

– brew tap homebrew/dupes– brew tap homebrew/versions– brew tap homebrew/homebrew-php– brew install php70 php70-intl php70-mcrypt– brew install docker docker-compose

© 2016 Magento, Inc. Page | 35

OS X Development with Docker - Dinghy

• Virtualbox (with extensions)• Install Dinghy

– brew tap codekitchen/dinghy– brew install dinghy

• Initialise dinghy for boot2docker (bump up performance)– bumped memory and cpus

• dinghy create \ --memory=4096 \ --cpus=2 --disk=40000 \ --provider=virtualbox

© 2016 Magento, Inc. Page | 36

Engage with the Magento community

• Twitter

• Sherrie Rohde community digest– https://community.magento.com/t5/News-Announcements/bd-p/newsannouncements

• Meet Magento events and hackathons

• Official Magento events (Live UK, Paris, Imagine Vegas)

© 2016 Magento, Inc. Page | 37

Thank you