22
start – let’s think of this simple // Route: hello?name=world $hello = $_GET[‘name’]; printf(‘Hello %s’ , $hello); // Outputs ‘world’ and a warning e would like to do is echo query string param to th Think about that!

Ran Mizrahi - Symfony2 meets Drupal8

Embed Size (px)

DESCRIPTION

Ran Mizrahi @ Israel's DrupalCamp talking about the upcoming adoption of Symfony2 components in Drupal 8

Citation preview

Page 1: Ran Mizrahi - Symfony2 meets Drupal8

For start – let’s think of this simple task!

// Route: hello?name=world

$hello = $_GET[‘name’];

printf(‘Hello %s’ , $hello); // Outputs ‘world’ and a warning

All we would like to do is echo query string param to the screen..

Think about that!

Page 2: Ran Mizrahi - Symfony2 meets Drupal8

Symfony2 meets Drupal 8

Author: Ran Mizrahi (@ranm8) Open Source Department Leader

Page 3: Ran Mizrahi - Symfony2 meets Drupal8

About CodeOasis

• CodeOasis specializes in advanced web solutions.

• Large variety of customers (from startups to retail companies and enterprises)

• Two main technology environments:• Open Source – PHP, JS and rich HTML5 and

CSS3 client side applications• Microsoft .NET

Page 4: Ran Mizrahi - Symfony2 meets Drupal8

Drupal is Awesome!!!

!?!?!!?

• End users - A way to create your own tailored made CMS - It’s great for our end users

• Salesmen - Known brand with many major use cases! - It’s great for a salesman

Page 5: Ran Mizrahi - Symfony2 meets Drupal8

Well, maybe just if your’e looking for a frameworkDrupal SUCKS for developers!!

Page 6: Ran Mizrahi - Symfony2 meets Drupal8

Drupal Started as a CMS and evolved to a CMF!

The Problems:

• Larger websites are developed using Drupal content management abilities – Lots of code is written.

• BIG learning curve for both junior and experienced developers – Learn the “Drupal Way” (Procedural AOP)

• Lack of web development drivers/components

• Legacy code – Well, like every 11 years old software..

Page 7: Ran Mizrahi - Symfony2 meets Drupal8

What is Symfony2??

"Symfony2 is a reusable set of standalone, decoupled, and cohesive PHP components that solves common web development problems.”

Fabian Potencier – Symfony’s project lead

Page 8: Ran Mizrahi - Symfony2 meets Drupal8

So, why use Syfmony2 in Drupal 8

• Symfony2 provides a great set of standalone, independent PHP components that solve common web problems. (HttpFoundation, ClassLoader, EventDispatcher, etc.)

• Move to the next generation PHP OOP features that are available from PHP 5.3 and above

• The PSR (PHP Standard Recommendation) standard – Allows easy-lazy-class-loader (by not bootstrapping code we don’t need).

Page 9: Ran Mizrahi - Symfony2 meets Drupal8

Symfony2 Components in Drupal 8

HttpFoundation BrowserKit

Console

Templating

ClassLoader

Yaml

Routing

HttpKernal

CssSelector

EventDispatcher

DependencyInjection

FinderConfig

FileSystem ProcessDomCrawler Validator

Security

Form

Page 10: Ran Mizrahi - Symfony2 meets Drupal8

PSR (PHP Recommended Standards)

• Set of PHP recommended standards by agreed and embraced by PHP most popular projects (Symfony, Drupal, Zend, CakePHP, Joomla, phpBB, etc.)

• Suggested coding standards for:• Naming conventions for autoloading (PSR-0)• Basic coding standards (PSR-1)• Coding style guide (PSR-2)

Page 11: Ran Mizrahi - Symfony2 meets Drupal8

ClassLoader

• ClassLoader loads your project classes automatically if they follow the PSR-0 standard naming convention

• One universal class autoloader that follows PHP known standards

• Lazy-loading-class-loader

Page 12: Ran Mizrahi - Symfony2 meets Drupal8

ClassLoader

require_once __DIR__ . ’/ClassLoader/UniversalClassLoader.php';

use Symfony\Component\ClassLoader\UniversalClassLoader;

$loader = new UniversalClassLoader();$loader->register();

// Now we can register some namespaces…$loader->registerNamespace(‘Symfony’, __DIR__ . ‘/src’);

Example

Page 13: Ran Mizrahi - Symfony2 meets Drupal8

HttpFoundation

• HttpFoundation wraps HTTP specification (PHP super globals - $_GET, $_SESSION, $_POST, $_COOKIE, $_FILE, etc. for request and echo, header, setcookie, etc. for response) with OO layer

• It provides abstraction for HTTP requests, responses, cookies, session, uploaded files, etc.

Page 14: Ran Mizrahi - Symfony2 meets Drupal8

HttpFoundation

// Route: hello?name=world

$hello = $_GET[‘name’];

printf(‘Hello %s’ , $hello); // Outputs ‘world’ and a warning

Let’s start with a simple task:

It turns out to be not that simple, now we’ve got PHP warning:

// Route: hello?name=world

$hello = isset($_GET[‘name’]) ? $_GET[‘name’] : ‘World’;

printf(‘Hello %s’ , $hello); // Outputs ‘world’

OK, what now?! Yes, we have XSS issue )-: Let’s fix…

Page 15: Ran Mizrahi - Symfony2 meets Drupal8

HttpFoundation

// Route: hello?name=world

$hello = isset($_GET[‘name’]) ? $_GET[‘name’] : ‘World’;

printf(‘Hello %s’ , htmlspecialchars($hello, END_QUOTES, ‘UTF-8’)); // Outputs ‘world’

Hard job for a simple task and makes our code look… UGLY!

Page 16: Ran Mizrahi - Symfony2 meets Drupal8

HttpFoundationThis is how our code will look like using HttpFoundation:

// /hello?name=worlduse Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;

$request = Request::createFromGlobals();echo ‘Hello’ . $request->query->get(‘name’ , ‘World’); // Outputs ‘world’

And it’s great for unit testing (-:class HelloTest extends \PHPUnit_Framework_TestCase {

public function testHello() {$request = Request::create('/?name=world', 'GET');$helloClass = new Hello();$content = $helloClass->sayHello();$this ->assertsEquals(’Hello world’, $content);

}

}

Page 17: Ran Mizrahi - Symfony2 meets Drupal8

EventDispatcher

• The EventDispatcher is lightweight implementation of the Observer design pattern

• Provides the ability to dispatch and listen to events.

use Symfony\Component\EventDispatcher\EventDispatcher;use Symfony\Component\EventDispatcher\Event;

$dispatcher = new EventDispatcher();

$dispatcher->addListener(‘new_node’ , function(Event $event) {// React to the event

});

$dispatcher->dispatch(‘new_node’);

Page 18: Ran Mizrahi - Symfony2 meets Drupal8

EventDispatcher

So how will it fit in Drupal (Larry Garfield’s prediction )

• Drupal 8 will have both hooks and EventDispatcher

• EventDispatcher will be closer to the core, hooks further out

• Drupal 9, maybe only event dispatcher ?!? I hope so (-:

Page 19: Ran Mizrahi - Symfony2 meets Drupal8

Templates - Twig (Maybe?)

What is Twig?

• Twig is a template engine for PHP

• Uses it’s own template syntax, originally inspired from Jinja and Django (According to Wikipedia)

• Symfony2 uses Twig as main template engine

• Twig was written by Fabian Potencier

Page 20: Ran Mizrahi - Symfony2 meets Drupal8

Templates - Twig (Maybe?)

PHPTemplate:

<div><?php if ($content): ?><span><?php echo $content; ?></span><span><?php echo htmlspecialchars($content, ENT_QUOTES, ‘UTF-8’); ?>

</span><?php endif; ?>

</div>

<div>{% if content %}<span>{{ content }}</span><span>{{ content|escape }}</span>{% endif %}

</div>

Twig:

Page 21: Ran Mizrahi - Symfony2 meets Drupal8

Templates - Twig (Maybe?)

Why Twig is good for us:

• Secure

• Front-end developer friendly (No PHP knowledge is required

• Not Drupal-proprietary.

• Great way to make sure that logics won’t find a place on Drupal templates (-:

Page 22: Ran Mizrahi - Symfony2 meets Drupal8

Questions!? (-:

Contact me @ranm8 on twitterMail : [email protected]