LAMP Meetup - Symfony & YUI3

Post on 06-May-2015

4.489 views 3 download

Transcript of LAMP Meetup - Symfony & YUI3

Dustin Whittle - dustin@yahoo-inc.com

Who am I?

Yahoo! – Application Platform + Dev Network

Worked with Y! Answers, Delicious, Y! Bookmarks, Y! Widgets, Yahoo! Application Platform

Working with symfony since open source

symfony Core Team Member

Responsible for symfony at Yahoo!

symfony-project.com | developer.yahoo.com/yui

Users

Load Balancers

Frontend

PHPAPC, PEAR, PECL, Custom Extensions

FreeBSD 4.x/6.x, Linux 2.6.x

ysymfony / YUIApacheCustom Modules

Backend

MySQL/Oracle Web Services Ad API User API

Why use a frontend platform?

Rasmus says “frameworks are not well suited for Y!”

Build applications to requirements

Do exactly what you need: no more, no less

Understand that frameworks add overhead

Choosing functional components is a better fit

Despite choosing open source or building your own

Everyone uses a framework

If you use open source, you get maintenance for free

Why use a framework?

Another software layer (symfony, php, apache)

Factors out common patterns

Code Layout, Configuration, URL Routing

ORM / Data Access

Authentication / Security (XSS/CSRF)

Form Widgets / Validation / Repopulation

Internationalization / Localization

Debugging and Testing utilities

Encourages good design

Abstraction > Consistency > Maintainability

YUI3

The syntax is more terse, allowing you to write more compact code using techniques like chaining.

The API is selector-driven, which allows you to target page elements flexibly.

The library is even more modular and granular than before, so you only put code on the page for features you need.

YUI3

Lighter

Don't load what you don't need

Don't write code more than once, use it again

Easier

Consistent API

Base, Selector, Widget, IO/Get/DataSource

Convenience

each, bind, nodelist, queue, chainability, general sugar

Faster

Opportunity to re-factor core performance pain points

A full stack MVC Framework

Written in OOP PHP5

Developed by a French company, Sensio Labs

Released open source in October 2005

Licensed under MIT license

A large community of active developers

A unit + functional testing library, LIME

What is symfony?

The Big Picture

A complete platform for building web applications from frameworks

PHP Framework

JavaScript Framework

CSS Framework

UI Design Patterns + Best Practices

Development Tools (logger, profiler, debugger, docs)

Unit + Functional Testing Frameworks (LIME /

Open Source Communities

Who uses ysymfony/yui?

symfony is a cohesive and decoupled set of PHP

classes

symfony Platform

The symfony framework is based on the symfony

platform

symfony Framework

Model-View-Controller

Some people think frameworks are slow and

trap you in a box.

Performance?

symfony in features

MVC Design + Project/Application Structure

Cascading Configuration System (YAML -> PHP)

Environments – Dev, Prod, Test, QA, Staging

Flexible view layer – layouts, partials, components

Task/CLI System

Internationalization + Localization Support

Form / Widget / Validation System

Security – CSRF + XSS Protection + User Management

Caching – APC, Memcache, File, Database

Testing Framework – LIME Unit + Functional Testing

Make your choice based on the requirements of the project

Pick a framework or just a component

Symfony vs ?

Getting Started

pear channel-discover pear.symfony-project.org

pear install symfony/symfony

mkdir ~/dev/ysfproject;

cd ~/dev/ysfproject

symfony generate:project ysfproject

symfony generate:app frontend

symfony generate:app frontend

symfony CLI + Tasks

sfEventDispatcher

sfPatternRouting and sfWebRequest are decoupled

« Anybody » can listen to any event

// sfPatternRouting$callback = array($this, ’filterParameters‘);$dispatcher->connect(’request.filter_parameters‘, $callback);

// sfWebRequest$event = new sfEvent($this, ‘request.filter_parameters’);$dispatcher->filter($event, $parameters);

application.log

application.throw_exception

context.load_factories

request.filter_parameters

user.change_culture

controller.page_not_found

response.filter_content

view.cache.filter_content

*.method_not_found

require '/home/y/share/pear/symfony/autoload/sfCoreAutoload.class.php';sfCoreAutoload::register();

$dispatcher = new sfEventDispatcher();

$routing = new sfPatternRouting($dispatcher);$routing->connect(new sfRoute('hello', '/hello/:name'));

$request = new sfWebRequest($dispatcher);$response = new sfWebResponse($dispatcher);

$content = 'Hello '.$request->getParameter('name', 'World');

$response->setContent($content);$response->send();

symfony Configuration

Based on YAML

Support for environments

prod, dev, test, qa, staging, ...

Dimension System

Allows easy customization of any configuration files and templates

Anything can be a dimension

Intl, Brand, Host, Data center

Dimensions are Flexible

Inheritance = Override only what you need to

Dimensions can be chained together

homepage: url: / param: { module: common, action: homepage }

item_get: url: /api/v1/item/:id param: { module: item, action: get} requirements: { id: ‘\d’ }

frontend/modules/common/actions/actions.class.php

class commonActions extends sfActions{ public function executeHomepage($request) { $this->templateFooVar = ‘Hello World’; }}

frontend/modules/common/templates/homepageSuccess.php

<?php echo $templateFooVar; ?>

symfony view layer

Actions can render different views, a partial, or a component

(think: same controller for ajax/full view)

View.yml

Configures css, js, meta, layout (app/module/action)

Layouts – Most web apps consist of full page and popup layout

A partial is a static template fragment

class SigninForm extends sfForm{ public function configure() { $this->setWidgets(array( 'username' => new sfWidgetFormInput(), 'password' => new sfWidgetFormInput(array('type' => 'password')), 'remember' => new sfWidgetFormInputCheckbox(), )); $this->setValidators(array( 'username' => new sfValidatorString(), 'password' => new sfValidatorString(), 'remember' => new sfValidatorBoolean(), )); $this->validatorSchema->setPostValidator(new UserValidator()); $this->widgetSchema->setNameFormat(’user[%s]'); }}

<form action=“<?php echo url_for(‘user/signin’);” method=“post”><?php echo $form; ?></form>

$this->form = new SigninForm();

if ($request->isMethod('post')){ $this->form->bind($request->getParameter('user')); if ($this->form->isValid()) { $values = $this->form->getValues(); $this->getUser()->signin($values['user']); }}

symfony / YUI

YUI integration is provided in the form of template helpers

AJAX

Buttons

Tabs

Overlays

Debug Toolbar Integration

Debugging Tools

Deployment Tools

Aggregate and minify stylesheets and javascripts

Rewrite templates, css, js for CDN (YCS)

Generate translations for configurations + templates + js

Aggregate core classes + hardcode paths + remove debug

Run lint, unit, functional tests

Deployment via rsync

LIME Unit Testing

./symfony test:unit

$t = new lime_test(9, new lime_output_color());

// create configuration$t->comment('check configuration');$frontendConfiguration = ProjectConfiguration::getApplicationConfiguration('frontend', 'test', true);$t->isa_ok($frontendConfiguration, 'frontendConfiguration', 'ProjectConfiguration::getApplicationConfiguration() returns a valid frontend application configuration instance.');

// check plugins enabled$t->comment('check application plugins');$t->is_deeply($frontendConfiguration->getPlugins(), array(1 => 'sfDoctrineGuardExtraPlugin', 2 => 'sfDoctrineGuardPlugin', 3 => 'sfDoctrineManagerPlugin', 4 => 'sfDoctrinePlugin', 5 => 'sfDoctrineViewCachePlugin', 6 => 'sfFacebookPlugin', 7 => 'sfFormExtraPlugin', 10 => 'sfTaskExtraPlugin', 11 => 'ysfOpenPlugin', 12 => 'ysfYUIPlugin'), 'frontendConfiguration->getPlugins() returns a valid list of application plugins.');

// create instance$t->comment('check context instance');$frontendContext = sfContext::createInstance($frontendConfiguration);$t->isa_ok($frontendContext, 'sfContext', 'sfContext::createInstance() returns a valid context instance from configuration.');

LIME Functional Testing

./symfony test:functional backend

$browser = new frontendTestFunctional(new sfBrowser());$browser->get('/')->

with('view_cache')->begin()-> isCached(false)-> end()->

with('user')->begin()-> isCulture('en')-> end()->

with('request')->begin()-> isParameter('module', 'stream')-> isParameter('action', 'activity')-> end()->

with('response')->begin()-> isStatusCode(200)-> isHeader('Content-Type', 'text/html; charset=utf-8')-> checkElement('style[type="text/css"]')-> checkElement('div#y-doc')-> checkElement('div.yui-skin-sam')-> checkElement('div#y-doc div#hd')-> checkElement('div#y-doc div#hd div#nav')-> checkElement('div#y-doc div#bd')-> checkElement('div#y-doc div#bd div#y-content')-> checkElement('div#y-doc div#ft')-> checkElement('script[type="text/javascript"]')->

end();

YUI Unit Testing

YUI({combine: true, timeout: 10000}).use("node", "console", "test",function (Y) { Y.namespace("example.test"); Y.example.test.AdvancedOptionsTestCase = new Y.Test.Case({ name: "Advanced Options Tests", _should: { fail: { testFail: true }, error: { testGenericError: true }, ignore : { testIgnore: true } }, testFail : function() { Y.Assert.fail("Something bad happened."); }, testGenericError : function() { throw new Error("Generic error"); }, testIgnore : function () { alert("You'll never see this."); } }); var r = new Y.Console({ verbose : true, newestOnTop : false }); r.render('#testLogger'); Y.Test.Runner.add(Y.example.test.AdvancedOptionsTestCase);

Y.Test.Runner.run();});

symfony Documentation

http://www.symfony-project.org/doc/

Practical symfony - Jobeet

The Definitive Guide to symfony

The symfony Reference Guide

symfony Forms in Action

Doctrine ORM for PHP

Cookbooks + Cheat sheets + Wiki

API Documentation

A set of 24 tutorials

Each tutorial is meant to last one hour, and explains the step by step development of a web application with symfony, from A to Z.

http://www.jobeet.org/

http://www.symfony-project.org/jobeet/1_2/

Forums

http://www.symfony-project.com/forum

IRC

irc.freenode.net/#symfony

Mailing List

symfony-users@googlegroups.com

Support

YUI Documentation

http://developer.yahoo.com/yui/3/

128 Functional Examples

API Documentation

A  Query  Language  for  the  Web

SELECT    *  FROM  INTERNET

Thousands of web services that provide valuable data

Require developers to read documentation and form URLs/queries.

Data is isolated and can not be combined

Needs combining, tweaking, shaping even after it gets to the developer.

Before YQL

SQL-Like Language

Synonymous with Data access

Familiar to developers

Expressive enough to get the right data

Self Describing - show, desc table

Allows you to query, filter and join data across Web Services.

Y! Open Stack – YQL

YQL – Open TablesTwitter

Google

Facebook

Friendfeed

Wesabe

Whitepages

Zillow

On github - http://github.com/yql/yql-tables/

More than 275 functional examples

http://developer.yahoo.com/yui/examples/

YSlow + Performance Rules

http://developer.yahoo.com/performance

YUI Blog

http://yuiblog.com/

Mailing List @ Yahoo! Groups

Y! Developer Network – Documentation