fluent development with flow3

48
Inspiring people to share International PHP Conference 2008 Fluent Development with FLOW3

Upload: robert-lemke

Post on 19-May-2015

993 views

Category:

Documents


1 download

DESCRIPTION

FLOW3 is a modern web application framework for PHP, developed as the foundation of the upcoming version of the TYPO3 CMS. It introduces new development concepts to the PHP world such as Domain-Driven Design, Dependency Injection and Aspect-Oriented Programming. In this session you’ll get a first-hand introduction to the concepts behind FLOW3.

TRANSCRIPT

Page 1: Fluent Development with FLOW3

Inspiring people toshare

International PHP Conference 2008Fluent Development with FLOW3

Page 2: Fluent Development with FLOW3

The History of FLOW3(short version)

Page 3: Fluent Development with FLOW3

Since 1998

33 core members

committed 500.000 lines of code

resulting in a code base of

300.000 lines today

The Long History of TYPO3

Page 4: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

TYPO3 todayTYPO3 v4 is nearly feature complete

Grown architecture, few unit tests

Fundamental changes are risky

Often used as an application framework - but was designed as a CMS

Page 5: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

TYPO3 tomorrow?

Page 6: Fluent Development with FLOW3

<?php

Page 7: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Buy none get two for free.

Page 8: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

TYPO3 tomorrowFLOW3 acts as a reliable basis for any kind of web application

TYPO3 v5 is a package based on FLOW3

Extensions are packages as well, all based on FLOW3

Packages can be used

as extensions for TYPO3

as libraries for standalone applications

Page 9: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

The FLOW3 experienceFlow [fl!] The mental state of operation in which the person is fully immersed in what he or she is doing by a feeling of energized focus, full involvement, and success in the process of the activity. Proposed by positive psychologist Mihály Csíkszentmihályi, the concept has been widely referenced across a variety of fields.

FLOW3 [fl!'three] The application framework which takes care of all hassle and lets you play the fun part.

Page 10: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

! /**

! * Creates a customer

! *

! * @return void

! */

! public function createAction() {

! ! if (!$this->authorizationService->getCurrentUser()->hasRole('Admin')) {

! ! ! $this->logger->log('Someone tried to create a new customer.');

! ! ! throw new AccessDeniedException('You may not create customers.');

! ! }

! ! if ($this->request->getProtocol != 'HTTPS') {

! ! ! $this->logger->log('Someone tried to create a new customer not using HTTPS.');

! ! ! throw new SecurityException('Customers may only be created via HTTPS');

! ! }

! !

! ! if (strlen($_POST['firstname']) > 50) throw new InvalidArgumentException();

! !

! ! $customer = new Model\Customer;

! ! $customer->setFirstName($_POST['firstname']);

! ! $customer->setLastName($_POST['lastname']);

! !

! ! $customerValidator = MyApp\Validators\CustomerValidator::getInstance();

! ! if ($customerValidator->validate($customer)) {

! ! ! $customer->save();

! ! ! $this->logger->log('A new customer was saved');

! ! } else {

! ! ! throw new InvalidCustomerException();

! ! }

! }

Page 11: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

! /**

! * Creates a customer

! *

! * @return void

! */

! public function createAction() {

! ! if (!$this->authorizationService->getCurrentUser()->hasRole('Admin')) {

! ! ! $this->logger->log('Someone tried to create a new customer.');

! ! ! throw new AccessDeniedException('You may not create customers.');

! ! }

! ! if ($this->request->getProtocol != 'HTTPS') {

! ! ! $this->logger->log('Someone tried to create a new customer not using HTTPS.');

! ! ! throw new SecurityException('Customers may only be created via HTTPS');

! ! }

! !

! ! if (strlen($_POST['firstname']) > 50) throw new InvalidArgumentException();

! !

! ! $customer = new Model\Customer;

! ! $customer->setFirstName($_POST['firstname']);

! ! $customer->setLastName($_POST['lastname']);

! !

! ! $customerValidator = MyApp\Validators\CustomerValidator::getInstance();

! ! if ($customerValidator->validate($customer)) {

! ! ! $customer->save();

! ! ! $this->logger->log('A new customer was saved');

! ! } else {

! ! ! throw new InvalidCustomerException();

! ! }

! }

Page 12: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

<?php

! /**

! * Creates a customer

! *

! * @return void

! */

! public function createAction() {

! ! if ($this->arguments->hasErrors()) $this->throwStatus(400, 'Bad Request', '<strong>Invalid arguments!</strong>');

! ! $customer = new Domain\Model\Customer();

! ! $this->dataMapper->map($this->arguments['customer'], $customer);

! ! $this->customerRepository->add($customer);

! ! $this->throwStatus(201);

! }

?>

Page 13: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

FLOW3 = Application FrameworkNot just a collection of components or code snippet library

Comes with ready-to-go default configuration

Package based

Runs with PHP 5.3 or later

Comes with a powerful JSR-283 based Content Repository

PHP 5.3alpha1

Page 14: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Finest Handmade PHP Code100% documented source code (top project at Ohloh)

Consistent and intuitive class, method and variable names

FLOW3 Core Team always develops test-driven

Continuous Integration

multiple commits each day

automatic tests for Linux, Windows and Mac with SQLite, MySQL and Postgres

open CI server with statistics, email and jabber notifications

Page 15: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

FLOW3 modulesAOP

Component

Configuration

Cache

Error

Event

Locale

Log

MVC

Package

Persistence

Property

Reflection

Resource

Security

Utility

Validation

... and more

Page 16: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Getting Started

Page 17: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Getting Started

RequirementsSome webserver (tested with Apache and IIS)

PHP 5.3alpha1 or higher (see http://snaps.php.net/)

PHP extensions: zlib, PDO and PDO SQLite and the usual stuff

Some database (tested with SQLite, MySQL and Postgres)

Page 18: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Getting Started

DownloadCurrently available through Subversion

Checkout the FLOW3 Distribution:svn co https://svn.typo3.org/FLOW3/distribution/trunk

or try the TYPO3 Distribution:svn co https://svn.typo3.org/TYPO3v5/distribution/trunk

Nightly builds will follow after the 1.0 alpha 1 release

Page 19: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Page 20: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Getting Started

Grant File PermissionsThe webserver needs

read access for all files of the distribution and

write access in the Public and Data directory

On Linux / Mac just call sudo ./fixpermissions.sh

On legacy operating systems: ask your system administrator

Page 21: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Getting Started

Create a packageIn order to create a new package, just createa new folder within the Packages directory.

Page 22: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Getting Started

Create a Default ControllerCreate a subfolder in your package: Classes/Controller/

Create the controller class file:

<?php

declare(ENCODING = 'utf-8');

namespace F3\MyPackage\Controller;

class DefaultController extends F3\FLOW3\MVC\Controller\ActionController {

! public function indexAction() {

! ! return 'Hello World!';

! }

}

?>

Page 23: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Bootstrap

Page 24: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Bootstrap

Public/index.phpThis file is the default main script

It launches FLOW3 in the Production context

The webserver's web root should point to the Public directory

define('FLOW3_PATH_PUBLIC', str_replace('\\', '/', __DIR__) . '/');require(FLOW3_PATH_PUBLIC . '../Packages/FLOW3/Classes/F3_FLOW3.php');

$framework = new F3\FLOW3();$framework->run();

Page 25: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Bootstrap

Public/index_dev.phpThis script is used for development

It launches FLOW3 in the Development context

More scripts like this can be created for additional contexts

define('FLOW3_PATH_PUBLIC', str_replace('\\', '/', __DIR__) . '/');require(FLOW3_PATH_PUBLIC . '../Packages/FLOW3/Classes/F3_FLOW3.php');

$framework = new F3\FLOW3('Development');$framework->run();

Page 26: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Model - View - Controller

Page 27: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

MVC

Key FeaturesPowerful Request-Response mechanism, based on Front Controller and Dispatcher

Very convenient controllers and views

Supports multiple template engines

Sophisticated, easy to configure routing

Built-in validation and default security

REST support

Page 28: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

MVC

Model TypesActive Record Domain Model

Model of / wrapper for a database table row

Model of a domain which consists of data and behaviour

is responsible for persistence doesn't know about persistence

mixes infrastructure concerns with model doesn't know about infrastructure

quick to implement without a framework clean and easy to use if framework supports it

Page 29: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

MVC Pattern

ModelActive Record Domain Model

Model of / wrapper for a database table row

Model of a domain which consists of data and behaviour

is responsible for persistence doesn't know about persistence

mixes infrastructure concerns with model doesn't know about infrastructure

quick to implement without a framework clean and easy to use if framework supports it

Page 30: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

MVC Pattern

ModelActive Record Domain Model

Model of / wrapper for a database table row

Model of a domain which consists of data and behaviour

is responsible for persistence doesn't know about persistence

mixes infrastructure concerns with model doesn't know about infrastructure

quick to implement without a framework clean and easy to use if framework supports it

Page 31: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Persistence

Page 32: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

JSR-283 based Content RepositoryDefines a uniform API for accessing content repositories

A Content Repository

is a kind of object database for storage, search and retrieval of hierarchical data

provides methods for versioning, transactions and monitoring

TYPO3CR is the first working port of JSR-170 / JSR-283

Karsten Dambekalns is member of the JSR-283 expert group

Persistence

Page 33: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Transparent PersistenceExplicit support for Domain-Driven Design

Class Schemata are defined by the Domain Model class

No need to write an XML or YAML schema definition

No need to define the database model and object model multiple times at different places

Automatic persistence in the JSR-283 based Content Repository

Legacy data sources can be mounted

Persistence

Page 34: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Components

Page 35: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Components

Component DependenciesComponents seldomly come alone

Components depend on other components which depend on other components which ...

Problem:

Components explicitly refer to other components:$phoneBookManager = new PhoneBookManager

Page 36: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Components

Dependency InjectionA component doesn't ask for the instance of another component but gets it injected

This methodology is referred to as the "Hollywood Principle":"Don't call us, we'll call you"

Enforces loose coupling and high cohesion

Makes you a better programmer

Page 37: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Components

AutowiringFLOW3 tries to autowire constructor arguments and arguments of inject* methods

The type of the component to be injected is determined by the argument type (type hinting)

Autowiring does not work with Setter Injection through regular setters (set* methods)

Dependencies are only autowired if no argument is passed explicitly

Page 38: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

DEMO

Page 39: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Security

Page 40: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Playground

Page 41: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Things to play with

F3BLOGTry out the Blog Example:svn co https://svn.typo3.org/FLOW3/Distribution/branches/BlogExample/

Page 42: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Things to play with

TYPO3CR AdminPlay with persistence and watch your object in the TYPO3CR Admin

Page 43: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Things to play with

TestrunnerExperiment with Test-Driven Development and watch the tests inFLOW3's test runner

Page 44: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Progress

Developing FLOW3 ...

Page 45: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Next StepsFirst FLOW3 alpha release end of this year

First pilot projects based on FLOW3 in spring '09

Further development of the TYPO3 package

Planned release of TYPO3 5.0 alpha: end of 2009

Page 46: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

LinksThese Slideshttp://flow3.typo3.org/documentation/slides/

FLOW3 Websitehttp://flow3.typo3.org

TYPO3 Forgehttp://forge.typo3.org

Further Readinghttp://flow3.typo3.org/about/principles/further-reading/

Page 47: Fluent Development with FLOW3

Inspiring people toshareFluent Development with FLOW3

Questions

Page 48: Fluent Development with FLOW3