bridging the gap - laracon 2013

134
Bridging the Gap Ben Corlett

Upload: ben-corlett

Post on 06-May-2015

1.389 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Bridging the Gap - Laracon 2013

Bridging the GapBen Corlett

Page 2: Bridging the Gap - Laracon 2013

Me

•Ben Corlett

•@ben_corlett

•Lead Developer at Cartalyst

Page 3: Bridging the Gap - Laracon 2013

Me

•Director of Webcomm

•Freelancer at Kapture

•Upcoming Tuts+ Premium Author

Page 4: Bridging the Gap - Laracon 2013

Australian

• I am not:

•Texan

•English

•Kiwi

Page 5: Bridging the Gap - Laracon 2013

Yes, I’ve Been Called All Those

Things.

Page 6: Bridging the Gap - Laracon 2013

Things I Make Myself Part of

•Sentry (with Bruno Gaspar & Daniel Petrie of Cartalyst).

•Laravel 4 (currently the biggest code contributor behind Taylor).

Page 7: Bridging the Gap - Laracon 2013

Things I Make Myself Part of

•Cartalyst’s Platform 1 / 2

•Approximately 1 billion Cartalyst packages

•OAuth 1 Client (League of Extraordinary Packages).

Page 8: Bridging the Gap - Laracon 2013

Things I Make Myself Part of

•PyroCMS

•FuelPHP (including various packages)

•There’s probably more…

Page 9: Bridging the Gap - Laracon 2013

Why Everybody Needs PSR-2

I’m kidding!

Page 10: Bridging the Gap - Laracon 2013

Composer!But first…

Page 11: Bridging the Gap - Laracon 2013

Pre-Composer

Page 12: Bridging the Gap - Laracon 2013

Pre-Composer

•You downloaded your framework of choice

•The framework did nearly everything - at least it attempted to.

Page 13: Bridging the Gap - Laracon 2013

The Framework Always Fell Short

Page 14: Bridging the Gap - Laracon 2013

The Framework Always Fell Short

•You made it work, by finding:

•Modules

•Extensions

•Packages

•Bundles

•Let’s call them all “add-ons”

Page 15: Bridging the Gap - Laracon 2013

What Went Wrong?

Page 16: Bridging the Gap - Laracon 2013

What Went Wrong?More frameworks appeared

Page 17: Bridging the Gap - Laracon 2013

What Went Wrong?You switched frameworks

Page 18: Bridging the Gap - Laracon 2013

What Went Wrong?

• I switched frameworks:

•CodeIgniter ~1.4

•Kohana ~3.0

•FuelPHP - days of “Carbon” and the awkwardly named “ThrustPHP”

•Laravel ~3.1

•Laravel 4

Page 19: Bridging the Gap - Laracon 2013

What Went Wrong?

•Your new framework still didn’t do all the things.

•You had a bunch of incompatible, near useless add-ons.

Page 20: Bridging the Gap - Laracon 2013

Life Went OnYour favourite add-ons were forked

Page 21: Bridging the Gap - Laracon 2013

Sentry

Page 22: Bridging the Gap - Laracon 2013

Sentry

•Sentry 1.0 for FuelPHP 1.x

•https://github.com/cartalyst/sentry/tree/fuelphp/1.0/master

Page 23: Bridging the Gap - Laracon 2013

Sentry

•Sentry 2.0 for FuelPHP 1.x

•https://github.com/cartalyst/sentry/tree/fuelphp/2.0/master

Page 24: Bridging the Gap - Laracon 2013

Sentry

•Sentry 2.1 for FuelPHP 1.x

•https://github.com/cartalyst/sentry/tree/fuelphp/2.1/develop

Page 25: Bridging the Gap - Laracon 2013

Sentry

•Sentry 1.0 for Laravel 3.x

•https://github.com/cartalyst/sentry/tree/v1.0.0

•Fork of Sentry 2.1 for FuelPHP

Page 26: Bridging the Gap - Laracon 2013

Sentry

•Sentry 1.1 for Laravel 3.x

•https://github.com/cartalyst/sentry/tree/1.1/master

Page 27: Bridging the Gap - Laracon 2013

OAuth 2

Page 28: Bridging the Gap - Laracon 2013

OAuth 2

•Kohana OAuth 2

•https://github.com/managedit/kohana-oauth2

Page 29: Bridging the Gap - Laracon 2013

OAuth 2

•CodeIgniter OAuth 2

•https://github.com/philsturgeon/codeigniter-oauth2

Page 30: Bridging the Gap - Laracon 2013

OAuth 2

•FuelPHP OAuth 2

•https://github.com/fuel-packages/fuel-oauth2

Page 31: Bridging the Gap - Laracon 2013

OAuth 2

•Laravel OAuth 2

•https://github.com/taylorotwell/laravel-oauth2

Page 32: Bridging the Gap - Laracon 2013

OAuth 2

•Code diverged

•Bug fixes not global

•Bug fixed on Kohana didn’t fix CodeIgniter, FuelPHP or Laravel.

•API changes meant re-learning add-ons

Page 33: Bridging the Gap - Laracon 2013

Scenario

•You now work with Laravel

•Client asks for you to integrate with Facebook on their legacy CodeIgniter site.

•You had to research CodeIgniter add-ons

•The job took 3x as long as it should

Page 34: Bridging the Gap - Laracon 2013

Scenario•You now work with Laravel

•Client asks for you to integrate with Tumblr on their legacy CodeIgniter site.

•You can’t find any suitable add-ons

•You resort to using PHP’s horrible cURL API

•Even worse, file_get_contents().

•The job took 5x as long as it should

Page 35: Bridging the Gap - Laracon 2013

That’s Grim.

Page 36: Bridging the Gap - Laracon 2013

Composer!

Page 37: Bridging the Gap - Laracon 2013

•De-framework

•De-couple

•Unify code

Composer

Page 38: Bridging the Gap - Laracon 2013

•5 flavours to 1

•https://github.com/cartalyst/sentry

•Any [or no] framework

•Deep integration with 4 major frameworks and native PHP

Sentry

Page 39: Bridging the Gap - Laracon 2013

•4 [known] flavours to 1

•https://github.com/php-loep/oauth2-client

•No deep integration with any framework

• It’s not necessary!

OAuth 2

Page 40: Bridging the Gap - Laracon 2013

I’m Not Finished

Page 41: Bridging the Gap - Laracon 2013

You’re Doing It Wrong.

Well, some of you are.

Page 42: Bridging the Gap - Laracon 2013

Packagist Says

•There’s around 540 packages tagged “laravel”

•A good portion of them have laravel/framework listed as a dependency.

•Many others are dependent on Laravel, even though it’s not specified.

Page 43: Bridging the Gap - Laracon 2013

Why?

Page 44: Bridging the Gap - Laracon 2013

Stop!You’re not doing anybody favours.

Page 45: Bridging the Gap - Laracon 2013

ScenarioYou’re a developer.

Page 46: Bridging the Gap - Laracon 2013

How Do You Implement a New

Feature In Your App?

Page 47: Bridging the Gap - Laracon 2013

3 Steps to Discovery

Page 48: Bridging the Gap - Laracon 2013

Step 1Find and implement a package and use as-is.

Page 49: Bridging the Gap - Laracon 2013

Step 1

•Best-case scenario

•Reduce time and workload

• Increase productivity

Page 50: Bridging the Gap - Laracon 2013

Step 2Package is missing required features

Page 51: Bridging the Gap - Laracon 2013

Step 2

•Send a pull request for missing features

•Help improve the package for others

•Give back to open source

Page 52: Bridging the Gap - Laracon 2013

Step 3Package works

But it’s not easy to setup with your framework

Page 53: Bridging the Gap - Laracon 2013

Step 3

•Send a pull request with integration

•Service provider

•Driver

•Bridging package

Page 54: Bridging the Gap - Laracon 2013

Why Make Your Own?

There are actually reasons for starting fresh

Page 55: Bridging the Gap - Laracon 2013

Fundamentally disagree with the

APIStart a community discussion first (GitHub

issue?)

Page 56: Bridging the Gap - Laracon 2013

StabilityIs the project too stable to accept the radical

changes you envision?

Page 57: Bridging the Gap - Laracon 2013

StubbornAre the developer(s) unwilling to consider your

suggestion for change?

Page 58: Bridging the Gap - Laracon 2013

It’s Okay to Re-Invent the Wheel

Taylor Otwell did it.

Page 59: Bridging the Gap - Laracon 2013

Laravel

•Sits on a lot of Symfony

•Symfony is:

•Stable

•Powerful

•Complex

•Pure

Page 60: Bridging the Gap - Laracon 2013

Laravel

•Laravel simplifies a lot

•Convention over configuration

•Lower learning curve

•Elegant, intuitive API

Page 61: Bridging the Gap - Laracon 2013

LaravelWould not be anywhere near as good without

Symfony.

Page 62: Bridging the Gap - Laracon 2013

Reasons Against Making Your Own

Page 63: Bridging the Gap - Laracon 2013

Reasons Against Making Your Own

•Time consuming

•Can split community

•Responsibility & obligations

Page 64: Bridging the Gap - Laracon 2013

Make It Easier for Me

Page 65: Bridging the Gap - Laracon 2013
Page 66: Bridging the Gap - Laracon 2013

Tips for Package Development

Page 67: Bridging the Gap - Laracon 2013

Forget Your Framework

Really.

Page 68: Bridging the Gap - Laracon 2013

Forget Your Framework

•Develop outside of your framework

•TDD is a great way to develop

•Alternatively, native (naked) PHP

Page 69: Bridging the Gap - Laracon 2013

Forget Your Framework

•Many packages only use Laravel’s:

• Input

•Config

•Validator

Page 70: Bridging the Gap - Laracon 2013

Interfaces

Page 71: Bridging the Gap - Laracon 2013

Interfaces

•Class and method dependencies should be interfaces.

•Allows for any implementation

Page 72: Bridging the Gap - Laracon 2013

Example 1Cartalyst’s “Data Grid” package

Page 73: Bridging the Gap - Laracon 2013
Page 74: Bridging the Gap - Laracon 2013
Page 75: Bridging the Gap - Laracon 2013
Page 76: Bridging the Gap - Laracon 2013

Example 2Sentry

Page 77: Bridging the Gap - Laracon 2013
Page 78: Bridging the Gap - Laracon 2013
Page 79: Bridging the Gap - Laracon 2013
Page 80: Bridging the Gap - Laracon 2013
Page 81: Bridging the Gap - Laracon 2013
Page 82: Bridging the Gap - Laracon 2013
Page 83: Bridging the Gap - Laracon 2013
Page 84: Bridging the Gap - Laracon 2013

Interfaces

•Built package framework-agnostic

•Then, integrate with framework through drivers (implementations)

Page 85: Bridging the Gap - Laracon 2013

Service Providers

Page 86: Bridging the Gap - Laracon 2013

Service Providers

•Should be used to setup your package

•Reduce work load for consumers

• Inject config here

Page 87: Bridging the Gap - Laracon 2013

Facades

Page 88: Bridging the Gap - Laracon 2013

Facades

•Don’t need to be complex

•Phill covered in his talk

Page 89: Bridging the Gap - Laracon 2013

Example

Page 90: Bridging the Gap - Laracon 2013
Page 91: Bridging the Gap - Laracon 2013
Page 92: Bridging the Gap - Laracon 2013

Facades

•Should only be referenced from an app level

Page 93: Bridging the Gap - Laracon 2013

Don’t Do That, Do This

Page 94: Bridging the Gap - Laracon 2013

Don’t

Page 95: Bridging the Gap - Laracon 2013
Page 96: Bridging the Gap - Laracon 2013
Page 97: Bridging the Gap - Laracon 2013

Do

Page 98: Bridging the Gap - Laracon 2013
Page 99: Bridging the Gap - Laracon 2013
Page 100: Bridging the Gap - Laracon 2013

Don’t Do That, Do This

•Never reference Facades in your package

•They’re specific to the standard Laravel 4 app.

• Inject config values through a service provider.

Page 101: Bridging the Gap - Laracon 2013

Don’t

Page 102: Bridging the Gap - Laracon 2013
Page 103: Bridging the Gap - Laracon 2013
Page 104: Bridging the Gap - Laracon 2013

Do

Page 105: Bridging the Gap - Laracon 2013
Page 106: Bridging the Gap - Laracon 2013
Page 107: Bridging the Gap - Laracon 2013
Page 108: Bridging the Gap - Laracon 2013

Don’t Do That, Do This

•Don’t use dependencies for the sake of using dependencies.

•Delegate request-specific code to app

•Delegate all app-specific code to app

Page 109: Bridging the Gap - Laracon 2013

Don’t

Page 110: Bridging the Gap - Laracon 2013
Page 111: Bridging the Gap - Laracon 2013

Do

Page 112: Bridging the Gap - Laracon 2013
Page 113: Bridging the Gap - Laracon 2013

Don’t Do That, Do This

•Validator isn’t always required for your packages.

•Localise errors in your app

Page 114: Bridging the Gap - Laracon 2013

Package Design

Page 115: Bridging the Gap - Laracon 2013

Package Design

•Drivers

•Bridging packages

Page 116: Bridging the Gap - Laracon 2013

Drivers

•Multiple implementations

• Interface-driven (ties into SOLID)

•“Plug & Play”

•Lower learning curve

Page 117: Bridging the Gap - Laracon 2013

Drivers

•Expose to larger audience

•Examples:

•Laravel’s database package

•Sentry

Page 118: Bridging the Gap - Laracon 2013

Drivers

•Multiple implementations

• Interface-driven (ties into SOLID)

•Examples:

•Laravel’s database package

•Sentry

Page 119: Bridging the Gap - Laracon 2013

Bridging Packages

•Tie or glue a package to a framework

•Keep core light

Page 120: Bridging the Gap - Laracon 2013

Bridging Packages

•Examples:

•Symfony’s Twig bundle

•Laravel’s Mail package

Page 121: Bridging the Gap - Laracon 2013

Bridging Packages Are Drivers

*Everybody gasps*

Page 122: Bridging the Gap - Laracon 2013

Bridging Packages Are Drivers

•Same code, different package

• It’s impossible to include drivers for all frameworks.

•Keeps package core light. Only download what you need.

Page 123: Bridging the Gap - Laracon 2013

Why Bother?

Page 124: Bridging the Gap - Laracon 2013

Why Bother?

•Help the community

•Community helps you

• Interoperability, and hence;

•Productivity

Page 125: Bridging the Gap - Laracon 2013

Why Bother?Why not?

Page 126: Bridging the Gap - Laracon 2013

It’s Easier to Be Lazy

Page 127: Bridging the Gap - Laracon 2013

It’s Easier to Be Lazy

•Not in the long run, it’s not

•Not for everybody using another framework, it’s not.

•Not when the framework changes, it’s not

Page 128: Bridging the Gap - Laracon 2013

Two Packages Are Slower

Page 129: Bridging the Gap - Laracon 2013

Two Packages Are Slower

•No, they’re not

•Autoloading doesn’t care if there’s 1 package for each class, it’s relative to the filesystem.

Page 130: Bridging the Gap - Laracon 2013

tl;dr

•Contribute to existing packages where possible.

Page 131: Bridging the Gap - Laracon 2013

tl;dr

•Forget all frameworks when making a package:

•De-couple packages

•Simplify code

•Speeds up a developer’s 3 steps of discovery.

Page 132: Bridging the Gap - Laracon 2013

tl;dr

•Code to allow for easy integration:

• Integrate through drivers

• Integrate through bridging packages

•Strengthen community:

•People working to make a package stronger helps everybody

Page 133: Bridging the Gap - Laracon 2013

tl;dr

•There’s a bunch of valid reasons for starting fresh.

•Starting fresh is a per-case basis

Page 134: Bridging the Gap - Laracon 2013

Thanks!

•Ben Corlett

•@ben_corlett

•Cartalyst, Webcomm