Silex and Twig. Jon Ginn

Similar documents
Software Engineering

Taming Wordpress Theming with Twig & Timber. Jonathan Ginn PHP Dorset Nov 2015

Introduction to Testing and Maintainable code

SOLID Principles. Equuleus Technologies. Optional Subheading October 19, 2016

Basic design patterns

Quick housekeeping Last Two Homeworks Extra Credit for demoing project prototypes Reminder about Project Deadlines/specifics Class on April 12th Resul

Build Testable Client and Service Applications

Backend Development. SWE 432, Fall Web Application Development

Design for Testability. Dave Liddament (Director and developer at Lamp Bristol Limited)

Ingegneria del Software Corso di Laurea in Informatica per il Management. Software quality and Object Oriented Principles

CIS192 Python Programming

Princeton University COS 333: Advanced Programming Techniques A Subset of PHP

Desktop Apps with PHP and Titanium. Ben Ramsey TEK X May 19, 2010

Lessons Learned. Johnny Bigert, Ph.D., Skype/Microsoft October 26, 2011

Principles of Ruby Applica3on Design. Dean Wampler Senior Mentor and Consultant Object Mentor, Inc. Chicago, IL

Single Responsibility Principle (SRP)

SYMFONY2 WEB FRAMEWORK

Smashing Node.JS: JavaScript Everywhere

Backend Development. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Index. Note: Boldface numbers indicate code and illustrations; an italic t indicates a table.

Web Scripting using PHP

The de constructed. Magento module

Front End Programming

17.11 Bean Rules persistent

Big Modular Java with Guice

CIS192 Python Programming

Mavrig. a Tcl application construction kit. Jean-Claude Wippler Equi 4 Software, NL. EuroTcl 2008, Strasbourg, FR

SOLID: Principles of OOD

welcome to BOILERCAMP HOW TO WEB DEV

Single Responsibility Principle

ROBOT OOP. Learning the basics of Object Oriented Programming using robots from popular culture

PHP XPDF Documentation

Course Syllabus. Course Title. Who should attend? Course Description. PHP ( Level 1 (

Object-Oriented Design I - SOLID

PHP Syntax. PHP is a great example of a commonly-used modern programming language.

Open Closed Principle (OCP)

About Me. Name: Jonathan Brown. Job: Full Stack Web Developer. Experience: Almost 3 years of experience in WordPress development

PHP-Einführung - Lesson 8 - Composer (Dependency manager) and JSON. Alexander Lichter June 27, 2017

1 Software Architecture

Index. Dmitri Nesteruk 2018 D. Nesteruk, Design Patterns in Modern C++,

09. Component-Level Design

Silex. Release 0.0.0

Backend Web Frameworks

Dependency Inversion, Dependency Injection and Inversion of Control. Dependency Inversion Principle by Robert Martin of Agile Fame

The Silex Book generated on July 7, 2018

Simple AngularJS thanks to Best Practices

The Silex Book generated on November 18, 2017

Conception Orientée Objets. Programmation SOLID

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

Node.js. Node.js Overview. CS144: Web Applications

Component-Level Design. Slides copyright 1996, 2001, 2005, 2009 by Roger S. Pressman. For non-profit educational use only

COURSE 2 DESIGN PATTERNS

Islandora and Fedora 4; The Atonement v3: The Atonermenter

ActiveNET. #202, Manjeera Plaza, Opp: Aditya Park Inn, Ameerpet HYD wwww.activenetinformatics.

Code review guide. Notice: Read about the language that you will test its code, if you don t have an idea about the language this will be difficult.

CSc 337 LECTURE 16: WRITING YOUR OWN WEB SERVICE

Intro to: Design Principles

Silex. Release 0.0.0

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

Clean Code Why Clean Code matters

Static Webpage Development

Application Architectures, Design Patterns

Building Websites with Zend Expressive 3

Getting started with Dependency Injection. Rob Allen May 2015

Clean Code * * Or why is more important how we write code rather what we write. Assoc. prof. Catalin Boja, Asist. Bogdan Iancu, Lect.

Hello everyone! Page 1. Your folder should look like this. To start with Run your XAMPP app and start your Apache and MySQL.

BPM + Mobile Building a hybrid mobile app for BPM IBM Corporation

CUTE GUTs for GOOD Good Unit Tests drive Good OO Design

Dependency Injection Container Documentation

Persistence. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Design Principles: Part 2

matross Release SNAPSHOT

5. Application Layer. Introduction

Test, Code, Design: Inverting the Waterfall

Outline. Design Principles: Part 2. e.g. Rectangles and Squares. The Liskov Substitution Principle (LSP) ENGI 5895: Software Design.

solid-principles #solidprinciples

Jonathan Haulund. AEHF Program Office United States Air Force. Reuse: Dealing With The Hand You re Dealt

Web Scripting using PHP

The Principled Developer. Gerardo

Architecting ios Project. Massimo Oliviero

PHPoC vs PHP > Overview. Overview

Clean Code in Small Companies

Philosophy of Unit Testing

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Laravel

CSCI 1320 Creating Modern Web Applications. Content Management Systems

Advanced WCF 4.0 .NET. Web Services. Contents for.net Professionals. Learn new and stay updated. Design Patterns, OOPS Principles, WCF, WPF, MVC &LINQ

Welcome to Design Patterns! For syllabus, course specifics, assignments, etc., please see Canvas

PHP + ANGULAR4 CURRICULUM 6 WEEKS

Object-Oriented Design

Summary of the course lectures

C++ Modern and Lucid C++ for Professional Programmers

PHPoC. PHPoC vs PHP. Version 1.1. Sollae Systems Co., Ttd. PHPoC Forum: Homepage:

Object-Oriented Design

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Separation of Concerns

EasyChair Preprint. Software Metrics Proposal for Conformity Checking of Class Diagram to SOLID Design Principles

AR.04 Composite Application Guidance for WPF (aka Prism ) Brian Noyes IDesign Inc (

Persistence & State. SWE 432, Fall 2016 Design and Implementation of Software for the Web

Scaffold Documentation

PHP CURRICULUM 6 WEEKS

Transcription:

Silex and Twig Jon Ginn

Silex and Twig Jon Ginn

Silex and Twig Alex Ross and Dave Hulbert

Alex Dave @rossey Senior engineer at Base* @dave1010 Tech lead at Base* *we re hiring wearebase.com

Alex Dave @rossey Senior engineer at Base* @dave1010 Tech lead at Base* *we re hiring wearebase.com

Silex and Twig

Silex

Micro-framework? a what?

Framework?...?

A structure for applications (and usually a library of tools too)

Silex a simple web application structure that doesn t get in your way library of tools = Symfony Components

Code!

// use the Request $name = $_GET['name']; // do some fun stuff $name = strtoupper($name); // send back a Response echo 'Hello '. htmlspecialchars($name);

tightly coupling :-( Tying your code to the input and output: Input, global states (Request) $_SERVER, $_GET, $_POST, $_REQUEST, $_FILES, $_COOKIE, $_ENV Output (Response) echo*, print, printf, die, exit *we could test this with ob_start, ob_get_clean

What if we wanted to make it a JSON API? send the response as an email or SMS? a commandline program? an enterprise SOAP Service? communicate via Morse code / Enigma machine?

Dirty code (STUPID) S - Singletons and Globals T - Tight Coupling U - Untestable code P - Premature Optimisation I - In-descriptive Naming D - Duplication D - Duplication

Testability (PHPUnit)

require_once DIR.'/../vendor/autoload.php'; $app = new Silex\Application(); $app->get('/hello/{name}', function($name) use($app) { return 'Hello '. $app->escape($name); }); $app->run();

#!/usr/bin/env ruby require 'sinatra' get '/hello/:name' do "Hello #{params[:name]}!" end

var express = require('express'); var app = express(); app.get('/hello.txt', function(req, res) { res.send('hello world'); });

Clean code Modular Reusable Easy to extend or change Easy to read and understand Easy to refactor (maintainable) Easy to test

SOLID S - SRP O - OCP L - LSP I - ISP D - DIP

SOLID S - Single Responsibility Principle O - Open / Closed Principle L - Liskov Substitution Principle I - Interface Segregation Principle D - Dependency Inversion Principle

3

3-ish

1.2

10

3

Single Responsibility Principle One reason to change

Open / Closed Principle Open for extension Closed for modification

Liskov Substitution Principle Extending a class shouldn t break stuff

Interface Segregation Principle Don t depend on stuff you don t use

Dependency Inversion Principle Depend on a concept (abstractions), not an implementation

Separation of concerns :-) GET Request object wraps $_SERVER, $_REQUEST, etc nicely RETURN application business logic Response object headers and body

A test (PHPUnit)

use Silex\WebTestCase; class ContactFormTest extends WebTestCase { public function testthecontactpage() { $client = $this->createclient(); $crawler = $client->request('get', '/contact'); $this->asserttrue($client->getresponse()->isok()); $this->assertcount(1, $crawler->filter('h1:contains("contact us")')); $this->assertcount(1, $crawler->filter('form')); $this->assertcount(1, $crawler->filter('input:contains("say hi!")')); } }

Make the test pass

HttpKernelInterface Creates flexible and fast HTTP-based applications

interface HttpKernelInterface { const MASTER_REQUEST = 1; const SUB_REQUEST = 2; public function handle( Request $name, $type = self::master_request, $catch = true ) {} }

/** * Handles a Request to convert it to a Response. * * When $catch is true, the implementation must catch all exceptions * and do its best to convert them to a Response instance. * * @param Request $request A Request instance * @param integer $type The type of the request (MASTER_REQUEST or SUB_REQUEST) * @param Boolean $catch Whether to catch exceptions or not * @return Response A Response instance * @throws \Exception When an Exception occurs during processing */

/** * Handles a Request to convert it to a Response. * * When $catch is true, the implementation must catch all exceptions * and do its best to convert them to a Response instance. * * @param Request $request A Request instance * @param integer $type The type of the request (MASTER_REQUEST or SUB_REQUEST) * @param Boolean $catch Whether to catch exceptions or not * @return Response A Response instance * @throws \Exception When an Exception occurs during processing */

http://stackphp.com

Used by Symfony Laravel Drupal phpbb

A Silex App *is* Pimple Simple container with ~80 lines of code ArrayAccess interface *IoC container, for dependency injection Allows you to loosely couple your classes *IoC = Inversion of Control

Twig

Better than mixing PHP & HTML

Separation

Variables {{ title }} {{ var escape }} {{ foo.bar }}

Control blocks {% for user in users %} * {{ user.name }} {% else %} No users have been found. {% endfor %}

Filters {{ name striptags title reverse }}

Includes {% include 'sidebar.html' %}

Inheritance base.twig <title>{% block title %}My site{% endblock %}</title> <div>{% block content %}{% endblock %}</div> child.twig {% extends "base.twig" %} {% block title %}Contact us{% endblock %} {% block content %}Email{% endblock %}

Twig in the wild

Silex & Twig Silex comes with a bridge that provides a Twig service Register the service and you get twig

"require": { "twig/twig": ">=1.8,<2.0-dev" } $app->register(new Silex\Provider\TwigServiceProvider(), array ( 'twig.path' => DIR. '/views', )); $app->get('/hello/{name}', function ($name) use ($app) { return $app['twig']->render('hello.twig', array( 'name' => $name, )); });

Silex in the wild WordPress Example

PHP Dorset site silex twig

Any Questions & Thank You Feedback: goo.gl/tnkect Sildes: goo.gl/ilu1rl Alex (@rossey) & Dave (@dave1010) wearebase.com/hiring Tweet Jon Ginn (@jonginn) and tell him what he missed!