PHP pod mikroskopom

Embed Size (px)

Citation preview

PHP

(@umpirsky)

PHP-

Namespaces

Closures

Traits

Namespaces

namespace Foo\Bar

use Foo\Bar

Closures

$double = function($a) { return $a * 2;};

Traits

class Base{ public function sayHello() { echo 'Hello '; }}

trait SayWorld{ public function sayHello() { parent::sayHello(); echo 'World!'; }}

class MyHelloWorld extends Base { use SayWorld;}

$o = new MyHelloWorld();$o->sayHello();

?

:

.

HTTP

GET /index.html HTTP/1.1Host: www.example.com

HTTP/1.1 200 OKDate: Mon, 23 May 2012 22:38:34 GMTServer: Apache/1.3.27 (Unix) (Red-Hat/Linux)Last-Modified: Wed, 08 Jan 2003 23:11:55 GMTContent-Type: text/html; charset=UTF-8...

Sinatra

Sinatra

require 'sinatra'

get '/hi' do "Hello World!"end

Silex

Silex

$app = new Silex\Application();

$app->get('/hello/{name}', function($name) { return 'Hello '.$name; });

$app->run();

Silex

$app->before(function() { // });

$app->after(function() { // });

$app->finish(function() { // });

Silex

$app = new Silex\Application();

$app->error(function (\Exception $e, $code) {//

});

Dependency Injection

Dependency Injection

class JsonUserPersister{ private $storage;

public function __construct(StorageInterface $storage) { $this->storage = $storage; }

public function persist(UserInterface $user) { $this->storage->store($user); }}

Pimple

Pimple

$container = new Pimple();

$container['cookie_name'] = 'SESSION_ID';$container['storage_class'] = 'SessionStorage';

$container['session_storage'] = function ($c) { return new $c['storage_class']($c['cookie_name']);};$container['session'] = function ($c) { return new Session($c['session_storage']);};

Silex extends Pimple

namespace Silex;

class Application extends \Pimple...

Silex Dependency Injection

$app = new Silex\Application();

$app['some_service'] = $app->share(function () { return new Service();});

Service Providers

$app = new Silex\Application();

$app->register(new UrlGeneratorServiceProvider());$app['url_generator']->generate('home');

Service Providers

Twig

URL Generator

Session

Validator

Form

HTTP Cache

Security

Swiftmailer

Monolog

Translation

Doctrine

class YourTest extends Silex\WebTestCase{ public function createApplication() { return require __DIR__.'/../../../app.php'; }

public function testFooBar() { // ... }}

public function testHomePage(){ $client = $this->createClient(); $crawler = $client->request('GET', '/');

$this->assertTrue($client->getResponse()->isOk()); $this->assertCount( 1, $crawler->filter('h1:contains("Contact us")') ); $this->assertCount(1, $crawler->filter('form')); // ...}

Twig

Twig

$app->register( new TwigServiceProvider(), array( 'twig.path' => __DIR__.'/views' ));

Twig

$app->get( '/hello/{name}', function ($name) use ($app) { return $app['twig']->render( 'hello.twig', array( 'name' => $name ));

});

Twig

{% extends 'layout.html.twig' %}

{% block content %} Hello {{ name }}{% endblock %}

Composer

{ "require": { "silex/silex": "1.*" }}

Composer

$ php composer.phar install

?

php.net

sinatrarb.com

silex.sensiolabs.org

pimple.sensiolabs.org

twig.sensiolabs.org

getcomposer.org