T3CON08 FLOW3 Tutorial

Post on 19-May-2015

1.539 views 0 download

Tags:

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

Inspiring people toshare

T3CON08Development with FLOW3

The History of FLOW3(short version)

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

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

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

Inspiring people toshareDevelopment with FLOW3

TYPO3 tomorrow?

Why start fromscratch?

Topictext

Source: The Secret Archives of TYPO3

”We need a new framework!“

Inspiring people toshareDevelopment with FLOW3

Buy none get two for free.

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

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.

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

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

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

Inspiring people toshareDevelopment with FLOW3

Getting Started

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)

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

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

Inspiring people toshareDevelopment with FLOW3

Getting Started

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

Inspiring people toshareDevelopment with FLOW3

Getting Started

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

Create the controller class file:

Inspiring people toshareDevelopment with FLOW3

Bootstrap

Inspiring people toshareDevelopment with FLOW3

Bootstrap

Public/

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();

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();

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

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

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

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

Inspiring people toshareDevelopment with FLOW3

Solving Beginner's Problems

Inspiring people toshareDevelopment with FLOW3

Topictext

Inspiring people toshareDevelopment with FLOW3

Model - View - Controller

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

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

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

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

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

Inspiring people toshareDevelopment with FLOW3

DEMO

Inspiring people toshareDevelopment with FLOW3

Validation

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

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)

Inspiring people toshareDevelopment with FLOW3

DEMO

Inspiring people toshareDevelopment with FLOW3

Components

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

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

Inspiring people toshareDevelopment with FLOW3

ExampleComponents

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!

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

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

Inspiring people toshareDevelopment with FLOW3

Components

Constructor without Dependency Injection

Inspiring people toshareDevelopment with FLOW3

Components

Component with Constructor Injection

Inspiring people toshareDevelopment with FLOW3

Components

Component with Setter Injection

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

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, ...);

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

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"

Inspiring people toshareDevelopment with FLOW3

Components

Component scope

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?

Inspiring people toshareDevelopment with FLOW3

Components

Creating PrototypesSolution A: Call getComponent()

Inspiring people toshareDevelopment with FLOW3

Components

Creating PrototypesSolution B: Call a factory method

Inspiring people toshareDevelopment with FLOW3

Components

Creating PrototypesPlanned feature: Automatically generated factory methods

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

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

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

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

Inspiring people toshareDevelopment with FLOW3

Persistence

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

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

Inspiring people toshareDevelopment with FLOW3

DEMO

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

Configuration

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

Configuration

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

Inspiring people toshareDevelopment with FLOW3

Configuration

Configuration Format

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

Inspiring people toshareDevelopment with FLOW3

Configuration

Configuration Types

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

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

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

Inspiring people toshareDevelopment with FLOW3

Configuration

Application Context

Inspiring people toshareDevelopment with FLOW3

REST Services

Inspiring people toshareDevelopment with FLOW3

Security

Inspiring people toshareDevelopment with FLOW3

Playground

Inspiring people toshareDevelopment with FLOW3

Things to play with

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

Inspiring people toshareDevelopment with FLOW3

Things to play with

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

Inspiring people toshareDevelopment with FLOW3

Things to play with

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

Inspiring people toshareDevelopment with FLOW3

Progress

Developing TYPO3 5.0 ...

Inspiring people toshareDevelopment with FLOW3

Current StateAOP

Component

Configuration

Cache

Error

Event

0 25 50 75 100

Inspiring people toshareDevelopment with FLOW3

Current StateLocale

Log

MVC

Package

Persistence

Property

0 20 40 60 80

Inspiring people toshareDevelopment with FLOW3

Current StateReflection

Resource

Session

Utility

Validation

0 25 50 75 100

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

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/

Inspiring people toshareDevelopment with FLOW3

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

Inspiring people toshareDevelopment with FLOW3

Questions