PHP под микроскопом




Саша Стаменковић (@umpirsky)
Новине у PHP-у

●   Namespaces
●   Closures
●   Traits
Namespaces

namespace FooBar


use FooBar
Closures

$double = function($a) {
     return $a * 2;
};
Traits
class Base                                class MyHelloWorld
{                                         extends Base
    public function sayHello()
    {                                     {
        echo 'Hello ';
    }
                                              use SayWorld;
}                                         }
trait SayWorld
{
                                          $o = new
    public function sayHello()
    {                                     MyHelloWorld();
        parent::sayHello();
                                          $o->sayHello();
        echo 'World!';
    }
}
µ
Зашто?
    Фрејмворк је обично:

●   тешко оружје
●   тежак за учење
Превише за обављање једноставних
            задатака.
HTTP протокол
GET /index.html HTTP/1.1
Host: www.example.com


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

require 'sinatra'


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

$app = new SilexApplication();


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


$app->run();
Silex

$app->before(function() {
      // пре обаде захтева
});


$app->after(function() {
      // после обаде захтева
});


$app->finish(function() {
      // после слања одговора
});
Silex


$app = new SilexApplication();


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


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


$app = new SilexApplication();


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

●   Twig              ●   Security
●   URL Generator     ●   Swiftmailer
●   Session           ●   Monolog
●   Validator         ●   Translation
●   Form              ●   Doctrine
●   HTTP Cache
Тестирање
class YourTest extends SilexWebTestCase
{
    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 %}
    <h1>Hello {{ name }}</h1>
{% 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
Хвала на пажњи

PHP pod mikroskopom