PHP pod mikroskopom

1,097 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,097
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

PHP pod mikroskopom

  1. 1. PHP под микроскопомСаша Стаменковић (@umpirsky)
  2. 2. Новине у PHP-у● Namespaces● Closures● Traits
  3. 3. Namespacesnamespace FooBaruse FooBar
  4. 4. Closures$double = function($a) { return $a * 2;};
  5. 5. Traitsclass 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!; }}
  6. 6. µ
  7. 7. Зашто? Фрејмворк је обично:● тешко оружје● тежак за учење
  8. 8. Превише за обављање једноставних задатака.
  9. 9. HTTP протоколGET /index.html HTTP/1.1Host: www.example.comHTTP/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...
  10. 10. Sinatra
  11. 11. Sinatrarequire sinatraget /hi do "Hello World!"end
  12. 12. Silex
  13. 13. Silex$app = new SilexApplication();$app->get(/hello/{name}, function($name) { return Hello .$name;});$app->run();
  14. 14. Silex$app->before(function() { // пре обаде захтева});$app->after(function() { // после обаде захтева});$app->finish(function() { // после слања одговора});
  15. 15. Silex$app = new SilexApplication();$app->error(function (Exception $e, $code) { // руковање грешкама});
  16. 16. Dependency Injection
  17. 17. Dependency Injectionclass JsonUserPersister{ private $storage; public function __construct(StorageInterface $storage) { $this->storage = $storage; } public function persist(UserInterface $user) { $this->storage->store($user); }}
  18. 18. Pimple
  19. 19. 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]);};
  20. 20. Silex extends Pimplenamespace Silex;class Application extends Pimple...
  21. 21. Silex Dependency Injection$app = new SilexApplication();$app[some_service] = $app->share(function () { return new Service();});
  22. 22. Service Providers$app = new SilexApplication();$app->register(new UrlGeneratorServiceProvider());…$app[url_generator]->generate(home);
  23. 23. Service Providers● Twig ● Security● URL Generator ● Swiftmailer● Session ● Monolog● Validator ● Translation● Form ● Doctrine● HTTP Cache
  24. 24. Тестирањеclass YourTest extends SilexWebTestCase{ public function createApplication() { return require __DIR__./../../../app.php; } public function testFooBar() { // ... }}
  25. 25. Тестирање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)); // ...}
  26. 26. Twig
  27. 27. Twig$app->register( new TwigServiceProvider(), array( twig.path => __DIR__./views ));
  28. 28. Twig$app->get( /hello/{name}, function ($name) use ($app) { return $app[twig]->render( hello.twig, array( name => $name )); });
  29. 29. Twig{% extends layout.html.twig %}{% block content %} <h1>Hello {{ name }}</h1>{% endblock %}
  30. 30. Composer{ "require": { "silex/silex": "1.*" }}
  31. 31. Composer$ php composer.phar install
  32. 32. Питања?
  33. 33. Литература● php.net● sinatrarb.com● silex.sensiolabs.org● pimple.sensiolabs.org● twig.sensiolabs.org● getcomposer.org
  34. 34. Хвала на пажњи

×