t3con08 flow3 tutorial

101
Inspiring people to share T3CON08 Development with FLOW3

Upload: robert-lemke

Post on 19-May-2015

1.543 views

Category:

Documents


0 download

DESCRIPTION

FLOW3 is a powerful and mondern web application framework for PHP. And FLOW3 is the foundation of TYPO3 version 5. The fact that you can develop both, TYPO3 extensions and standalone applications based on the same framework allows for reusing your code, knowledge and infrastructure in a bigger variety of projects.FLOW3 introduces many new development concepts to the PHP world, but still makes development much easier even without having to know them. However, a golden rule says: "Don’t fight your framework" - so it makes a lot of sense to understand the concepts behind FLOW3 in order to tap its full potential.In this is tutorial the participants got a first-hand introduction to the concepts behind FLOW3

TRANSCRIPT

Page 1: T3CON08 FLOW3 Tutorial

Inspiring people toshare

T3CON08Development with FLOW3

Page 2: T3CON08 FLOW3 Tutorial

The History of FLOW3(short version)

Page 3: T3CON08 FLOW3 Tutorial

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: T3CON08 FLOW3 Tutorial

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 5: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

TYPO3 todayTYPO3 v4 is nearly feature complete

Focus of the last releases were usbility improvements for the users

Grown architecture, no unit tests

Fundamental changes are risky / impossible

Often used as application framework - but it's a CMS

Page 6: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

TYPO3 tomorrow?

Page 7: T3CON08 FLOW3 Tutorial

Why start fromscratch?

Page 8: T3CON08 FLOW3 Tutorial

Topictext

Source: The Secret Archives of TYPO3

Page 9: T3CON08 FLOW3 Tutorial

”We need a new framework!“

Page 10: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Buy none get two for free.

Page 11: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment 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 12: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment 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 13: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment 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

Page 14: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Get the FLOW experienceIntuitive APIs

Readable source code (like a book)

Consistent naming for classes, methods and properties

Focus on the essential, the framework takes care of the infrastructure

Page 15: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

FLOW3 modulesAOP

Component

Configuration

Cache

Error

Event

Locale

Log

MVC

Package

Persistence

Property

Reflection

Resource

Session

Utility

Validation

... and more

Page 16: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Getting Started

Page 17: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Getting Started

RequirementsSome webserver (tested with Apache and IIS)

PHP 5.3 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: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment 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 as soon as we've set up our release mechanism

Page 19: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment 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 20: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Getting Started

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

Page 21: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Getting Started

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

Create the controller class file:

Page 22: T3CON08 FLOW3 Tutorial
Page 23: T3CON08 FLOW3 Tutorial
Page 24: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Bootstrap

Page 25: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Bootstrap

Public/

Page 26: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment 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 27: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment 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 28: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Bootstrap

$FLOW3->run()run() is a convenience method which

initializes the FLOW3 framework

resolves a request handler

handles and responses to the request

Page 29: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Bootstrap

$FLOW3->initialize()The initialization process is divided into different stages:

Initialize FLOW3

Initialize the packages

Initialize the components

Initialize the settings

Initialize the resources

The configuration for each level can't be changed once the initialization level is reached

Page 30: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Bootstrap

Beware of CachingDon't forget to run FLOW3 in Development context while you're developing because

component configuration is cached in production mode, so new classes won't be recognized

resources are cached in production mode, so changes won't be detected

and many more things might be cached which lead to unexpected errors if you change some code in your package

Page 31: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Bootstrap

Just in CaseIf you need to clear the cache, just remove all files from the Data/Temporary/ directory

Yes, there will be a backend function and command line tool for this

Page 32: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Solving Beginner's Problems

Page 33: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Topictext

Page 34: T3CON08 FLOW3 Tutorial
Page 35: T3CON08 FLOW3 Tutorial
Page 36: T3CON08 FLOW3 Tutorial
Page 37: T3CON08 FLOW3 Tutorial
Page 38: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Model - View - Controller

Page 39: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

The MVC Pattern

Modelan object which contains data and business logic of a certain domain

doesn't contain any information about the presentation of that data, but rather defines the behaviour

in the FLOW3 project we prefer a special kind of model, the Domain Model

Page 40: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

The MVC Pattern

Viewrepresents the display of the model on the web or another output channel

views only display data, they don't build or modify it

Page 41: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

The MVC Pattern

Controllerreacts on user input, selects and manipulates the model as accordingly

selects a view and passes it the prepared model for rendering

Page 42: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Action ControllerAn action controller

accepts a request

evaluates arguments

calls the action defined in the request

and adds output to the response

MVC

Page 43: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Action Controller: Important MethodsActions - methods just need an "Action" suffix:public function indexAction() { ... }public function deleteAction() { ... }

Initialization for the whole controller:public function initializeController() { ... }

Initialization of arguments:public function initializeArguments() { ... }

Initialization before any action is called:public function initializeAction() { ... }

MVC

Page 44: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

DEMO

Page 45: T3CON08 FLOW3 Tutorial
Page 46: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Validation

Page 47: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Validating ArgumentsAll arguments passed to an Action Controller (more precisely: Request Handling Controller) are automatically validated

White List policy: Only registered arguments are available

Accessing the $_GET and $_POST super globals is dangerous, dirty, deprecated and will probably be intercepted in the future

Page 48: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Validating ArgumentsFLOW3 comes with a bunch of built in validators:

AlphaNumeric, EmailAddress, Float, Integer, NotEmpty, Number, NumberRange, RegularExpression, UUID, Text

Custom validators can be created (especially for Domain Models)

All validators can be chained (and nested)

Page 49: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

DEMO

Page 50: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Components

Page 51: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

ComponentsComponents are re-usable, properly encapsulated objects

The lifecycle of a component and the combination of active components is managed by the Component Manager

All classes in the TYPO3 context are considered as components

Components are configurable

Components

Page 52: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Class ! ComponentClasses are automatically registered as components if

they reside in the Classes directory of a package and

their name follows the FLOW3 naming conventions

Components

Page 53: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

ExampleComponents

Page 54: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Components

Playing with building blocksThe combination of components used is configurable(orchestration)

The less components know about each other the easier it is to reuse them in a variety of contexts

Create your own LEGO set by creating cleanly separated, decoupled components!

Page 55: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment 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 56: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment 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 57: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Components

Constructor without Dependency Injection

Page 58: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Components

Component with Constructor Injection

Page 59: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Components

Component with Setter Injection

Page 60: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Components

AutowiringFLOW3's framework 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 61: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Components

Fetching components manuallyAlthough Dependency Injection is strongly recommended, there might be cases in which components need to be created or retrieved manually

Use the getComponent() method in these cases.

$component = $componentManager->getComponent($componentName, $arg1, $arg2, ...);

Page 62: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Components

Component scopeComponent objects always live in a certain scope

Currently supported scopes are:

Singleton - Only one instance exists during one script run

Prototype - Each getComponent() call returns a fresh instance

Page 63: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Components

Component scopeThe scope can be defined through

an annotation in the component class (recommended)

through the component configuration in a Components.php file

The default scope is "Singleton"

Page 64: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Components

Component scope

Page 65: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Components

Creating PrototypesDependency Injection can be used in almost any case, there's no need to call getComponent()

But what if you need to instantiate a component within a method?

Page 66: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Components

Creating PrototypesSolution A: Call getComponent()

Page 67: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Components

Creating PrototypesSolution B: Call a factory method

Page 68: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Components

Creating PrototypesPlanned feature: Automatically generated factory methods

Page 69: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Domain Driven DesignA domain is the activity or business of the user

Domain Driven Design is about

focussing on the domain and domain logic

accurately mapping the domain concepts to software

forming a ubiquitous language among the project members

Page 70: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Domain Driven Design

Ubiquitous languageThe common vocabulary is an important prerequisitefor successful collaboration

Use the same words for discussion, modeling, developmentand documentation

Page 71: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Domain Driven Design

Layered Architecture

Presentation

Domain

Data source

Application Logic (Service Layer)

Domain Model (Domain Layer)

View

Controller

Data Mapper (part of Content Repository)

Data Source Abstraction

Page 72: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Domain Driven Design

Layered Architecture

Presentation

Domain

Data source

Application Logic (Service Layer)

Domain Model (Domain Layer)

View

Controller

Data Mapper (part of Content Repository)

Data Source Abstraction

Page 73: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Persistence

Page 74: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment 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 75: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment 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 76: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

DEMO

Page 77: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3 Inspiring people toshareHitchhiker's Guide to FLOW3

Configuration

Page 78: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3 Inspiring people toshareHitchhiker's Guide to FLOW3

Configuration

Page 79: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Configuration

Configuration FormatThe default configuration format is PHP

Configuration options reside in a configuration object

The configuration object provides array access and a fluent interface

Configuration options are self-documenting

Page 80: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Configuration

Configuration Format

Page 81: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Configuration

Configuration TypesFLOW3 distinguishes between different configuration types for different purposes:

FLOW3 - reserved for FLOW3 configuration

Package - package related configuration

Component - configuration for components, including Dependency Injection

Routes - special configuration for defining MVC routes

Page 82: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Configuration

Configuration Types

Page 83: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Configuration

The CascadeEach package defines possible configuration options by setting default values

Default configuration can be altered by user-defined configuration files

User configuration can only modify existing configuration options

Modifying non-existent configuration options results in an error

Page 84: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Configuration

Application ContextAn application context is a set of configuration for a specific context

FLOW3 is shipped with configuration for these contexts:

Production

Development

Testing

Staging

FLOW3 is always launched in one defined context

Page 85: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Configuration

Application ContextConfiguration defined in the top level of a Configuration directory is the base configuration

Specialized configuration for application contexts reside in subdirectories named after the context

Application context configuration overrides the base configuration

Page 86: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Configuration

Application Context

Page 87: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

REST Services

Page 88: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Security

Page 89: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Playground

Page 90: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Things to play with

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

Page 91: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Things to play with

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

Page 92: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Things to play with

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

Page 93: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Progress

Developing TYPO3 5.0 ...

Page 94: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Current StateAOP

Component

Configuration

Cache

Error

Event

0 25 50 75 100

Page 95: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Current StateLocale

Log

MVC

Package

Persistence

Property

0 20 40 60 80

Page 96: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Current StateReflection

Resource

Session

Utility

Validation

0 25 50 75 100

Page 97: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Next StepsFirst FLOW3 beta release end of this year

First pilot projects based on FLOW3 in winter '08 / spring '09

Further development of the CMS package

Planned release of TYPO3 5.0 beta: end of 2009

Page 98: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

LinksFLOW3 Websitehttp://flow3.typo3.org

TYPO3 Forgehttp://forge.typo3.org

Coding Guidelineshttp://flow3.typo3.org/documentation/coding-guidelines/

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

Page 99: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

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

Page 100: T3CON08 FLOW3 Tutorial

Inspiring people toshareDevelopment with FLOW3

Questions

Page 101: T3CON08 FLOW3 Tutorial