Bernhard Schussek · webmozart.io 1/114
https://www.flickr.com/photos/emmacherry/2207748365 (CC BY-NC 2.0)
Play PHP Like A Puzzle
Bernhard Schussek (@webmozart)
Dutch PHP Conference 2016, Amsterdam
Slide 2
Slide 2 text
Bernhard Schussek · webmozart.io 3/114
Bernhard Schussek
Symfony Core Developer
since 2009
Slide 3
Slide 3 text
Bernhard Schussek · webmozart.io 4/114
@webmozart
Slide 4
Slide 4 text
Bernhard Schussek · webmozart.io 5/114
Bernhard Schussek
Developer of Puli
since 2014
Slide 5
Slide 5 text
Bernhard Schussek · webmozart.io 6/114
Bernhard Schussek
PHP Trainer and Coach
Symfony
Architecture
Coding Practices
webmozart.io
Slide 6
Slide 6 text
Bernhard Schussek · webmozart.io 7/114
Successful
Software Development
Slide 7
Slide 7 text
Bernhard Schussek · webmozart.io 8/114
https://www.flickr.com/photos/sthomasphotos/10205212344 (CC BY-NC-ND 2.0)
Speed of Development
Slide 8
Slide 8 text
Bernhard Schussek · webmozart.io 9/114
https://www.flickr.com/photos/_flood_/6468131271 (CC BY-NC-ND 2.0)
Quality of Development
Bernhard Schussek · webmozart.io 11/114
https://www.flickr.com/photos/deanhochman/8519852257 (CC BY 2.0)
Off-the-Shelve Solutions
Standard Applications, SaaS, …
Bernhard Schussek · webmozart.io 13/114
Libraries in PHP are
well established
Slide 13
Slide 13 text
Bernhard Schussek · webmozart.io 14/114
(since 2003) (since 2012)
Distribution as “Packages”
Slide 14
Slide 14 text
Bernhard Schussek · webmozart.io 15/114
Steps to Using a Package
Slide 15
Slide 15 text
Bernhard Schussek · webmozart.io 16/114
Step 1: Search Candidates
Packagist
Slide 16
Slide 16 text
Bernhard Schussek · webmozart.io 17/114
Step 2: Research Quality
Commits, Contributors, Activity, Tests, …
Slide 17
Slide 17 text
Bernhard Schussek · webmozart.io 18/114
$ composer require league/tactician
Using version ^1.0 for league/tactician
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing league/tactician (v1.0.2)
Downloading: 100%
Writing lock file
Generating autoload files
Step 3: Install in Your Project
Composer
Slide 18
Slide 18 text
Bernhard Schussek · webmozart.io 19/114
https://www.flickr.com/photos/sthomasphotos/10205212344 (CC BY-NC-ND 2.0)
$speed++
Slide 19
Slide 19 text
Bernhard Schussek · webmozart.io 20/114
Interoperable Packages
Slide 20
Slide 20 text
Bernhard Schussek · webmozart.io 21/114
use Symfony\Component\EventDispatcher\EventDispatcher;
$repository = new EventDispatcher();
PSR-1/4 Autoloading
Slide 21
Slide 21 text
Bernhard Schussek · webmozart.io 22/114
use Monolog\Logger;
use League\Tactician\Logger\LoggerMiddleware;
$logger = new Logger('my_app');
$commandBus = new CommandBus([
new LoggerMiddleware($formatter, $logger),
// ...
]);
PSR-3 Logging
Slide 22
Slide 22 text
Bernhard Schussek · webmozart.io 23/114
use Psr\Http\Message\ServerRequestInterface;
function showPosts(ServerRequestInterface $request)
{
// ...
}
PSR-7 Http Messages
Slide 23
Slide 23 text
Bernhard Schussek · webmozart.io 24/114
Reduce Coupling
Between Packages
Slide 24
Slide 24 text
Bernhard Schussek · webmozart.io 25/114
Choose the Best Package
for your purpose
Slide 25
Slide 25 text
Bernhard Schussek · webmozart.io 26/114
https://www.flickr.com/photos/superlekker/5917559189 (CC BY-NC 2.0)
Slide 26
Slide 26 text
Bernhard Schussek · webmozart.io 27/114
https://www.flickr.com/photos/_flood_/6468131271 (CC BY-NC-ND 2.0)
$quality++
Bernhard Schussek · webmozart.io 41/114
https://www.flickr.com/photos/rodenberger/12465588223 (CC BY 2.0)
$quality--
Slide 41
Slide 41 text
Bernhard Schussek · webmozart.io 42/114
https://www.flickr.com/photos/aftab/4153613907 (CC BY-NC 2.0)
$speed--
Slide 42
Slide 42 text
Bernhard Schussek · webmozart.io 43/114
https://www.flickr.com/photos/picturesfromwords/9539461080 (CC BY-NC 2.0)
Goal:
Interoperable Modules
for PHP
https://www.flickr.com/photos/emmacherry/2207748365 (CC BY-NC 2.0)
Slide 43
Slide 43 text
Bernhard Schussek · webmozart.io 44/114
Movement N° 1:
PSR-11 Standardized
DI Configuration
Slide 44
Slide 44 text
Bernhard Schussek · webmozart.io 45/114
David Négrier Matthieu Napoli
Slide 45
Slide 45 text
Bernhard Schussek · webmozart.io 46/114
class MyServiceProvider implements
ServiceProviderInterface
{
public function getServices()
{
return [
'service_name' => function () {
// create service
}
];
}
}
Providing Services from Modules
Slide 46
Slide 46 text
Bernhard Schussek · webmozart.io 47/114
'post_repository' => function () {
return new PDOPostRepository();
}
$container = new Pimple();
$container->addServiceProvider(new PDOServiceProvider())
$container->addServiceProvider(new MyServiceProvider());
$repo = $container->get('post_repository');
Example: PDOPostRepository
Slide 47
Slide 47 text
Bernhard Schussek · webmozart.io 48/114
'post_repository' => function (ContainerInterface $c) {
return new PDOPostRepository(
$c->get('pdo')
);
}
Service Injection
Slide 48
Slide 48 text
Bernhard Schussek · webmozart.io 49/114
'pdo' => function ($c, callable $getPrevious) {
return new PDOLogger(
$getPrevious(),
$c->get('logger')
);
}
Service Decoration
Slide 49
Slide 49 text
Bernhard Schussek · webmozart.io 50/114
'pdo.options' => function ($c, callable $getPrevious) {
return array_replace($getPrevious(), [
'database_name' => 'mydb',
]);
}
Service Parameters
Slide 50
Slide 50 text
Bernhard Schussek · webmozart.io 51/114
'twig_extensions' => function ($c, callable $getPrevious
return array_merge($getPrevious(), [
new MyTwigExtension(),
]);
}
Service Lists
Slide 51
Slide 51 text
Bernhard Schussek · webmozart.io 52/114
PostRepository::class => function (ContainerInterface $c
return new PDOPostRepository(
$c->get(PDO::class)
);
}
$repo = $container->get(PostRepository::class);
Interfaces as Service Names
Slide 52
Slide 52 text
Bernhard Schussek · webmozart.io 53/114
Benefits of the Proposal
Slide 53
Slide 53 text
Bernhard Schussek · webmozart.io 54/114
Simple
Slide 54
Slide 54 text
Bernhard Schussek · webmozart.io 55/114
Interoperable
Slide 55
Slide 55 text
Bernhard Schussek · webmozart.io 56/114
Tested with
Existing Containers
Symfony DI Adapter
Laravel Adapter
Pimple fork
PHP-DI
Bernhard Schussek · webmozart.io 84/114
Gulpfile.js
var gulp = require('gulp'),
puli = require('puli').load();
gulp.task('vendor', function () {
puli.src('/batman/blog/public/**/*')
.dest('res/public/blog');
});
Slide 84
Slide 84 text
Bernhard Schussek · webmozart.io 85/114
Generate URLs
var urls = require('puli-urls');
gulp.task('vendor', function () {
puli.src('/batman/blog/public/**/*')
.pipe(urls())
.dest('res/public/blog')
.pipe(urls.mapping())
.dest('.puli/urls.json');
});
Slide 85
Slide 85 text
Bernhard Schussek · webmozart.io 86/114
$generator = new JsonUrlGenerator('.puli/urls.json');
$generator->generateUrl('/acme/blog/public/style.css');
// => /blog/style.css
Print URLs in PHP
Slide 86
Slide 86 text
Bernhard Schussek · webmozart.io 87/114
Slide 87
Slide 87 text
Bernhard Schussek · webmozart.io 88/114
body {
background-image: asset_url("/res/public/bg.png");
// Relative paths
background-image: asset_url("bg.png");
}
Print URLs in CSS
Slide 88
Slide 88 text
Bernhard Schussek · webmozart.io 89/114
Discovery
“Let Modules Find Each Other“
Slide 89
Slide 89 text
Bernhard Schussek · webmozart.io 90/114
my/app-or-module
routes.php
vendor/router
Router
Slide 90
Slide 90 text
Bernhard Schussek · webmozart.io 91/114
my/app-or-module
messages.de.xlf
vendor/translator
Translator
Slide 91
Slide 91 text
Bernhard Schussek · webmozart.io 92/114
my/app-or-module
BlogPost.xml
vendor/orm
ObjectMapper
Slide 92
Slide 92 text
Bernhard Schussek · webmozart.io 93/114
my/app-or-module vendor/di-container
Container
MyServiceProvider
Slide 93
Slide 93 text
Bernhard Schussek · webmozart.io 94/114
{
"provide": {
"/app/routing.yml": "symfony/routing-yml",
"/app/trans/*.xlf": "league/translation-xlf",
"My\\AppServiceProvider":
"Psr\\DI\\ServiceProviderInterface"
}
}
puli.json (of your application or module)
Slide 94
Slide 94 text
Bernhard Schussek · webmozart.io 95/114
$router = new Router($discovery);
$translator = new Translator($discovery);
$container = new Container($discovery);
// ...
Using the Modules
Slide 95
Slide 95 text
Bernhard Schussek · webmozart.io 96/114
https://www.flickr.com/photos/sthomasphotos/10205212344 (CC BY-NC-ND 2.0)
$speed++
Slide 96
Slide 96 text
Bernhard Schussek · webmozart.io 97/114
https://commons.wikimedia.org/wiki/File:View_from_Volcano_Pacaya,_Guatemala.jpg (CC BY-SA 4.0)
Outlook
Slide 97
Slide 97 text
Bernhard Schussek · webmozart.io 98/114
Interoperable Modules
Have Potential
Slide 98
Slide 98 text
Bernhard Schussek · webmozart.io 99/114
for Sharing Modules
Between Frameworks
Slide 99
Slide 99 text
Bernhard Schussek · webmozart.io 100/114
https://www.flickr.com/photos/_flood_/6468131271 (CC BY-NC-ND 2.0)
$quality++
Slide 100
Slide 100 text
Bernhard Schussek · webmozart.io 101/114
for Enabling
Module-Based Frameworks
(vs. Component-Based)
Slide 101
Slide 101 text
Bernhard Schussek · webmozart.io 102/114
for Writing Apps
Without A Framework
Slide 102
Slide 102 text
Bernhard Schussek · webmozart.io 103/114
$container = new Container($discovery);
$router = $container->get(Router::class);
$controller = $router->getController('/hello/world');
$response = $controller->handle($request);
Applications with Puli and PSR-11
Slide 103
Slide 103 text
Bernhard Schussek · webmozart.io 104/114
Status of PSR-11
Slide 104
Slide 104 text
Bernhard Schussek · webmozart.io 105/114
Soon to enter Entrance Vote
Slide 105
Slide 105 text
Bernhard Schussek · webmozart.io 106/114
Status of Puli
Slide 106
Slide 106 text
Bernhard Schussek · webmozart.io 107/114
1.0.0-beta10
Slide 107
Slide 107 text
Bernhard Schussek · webmozart.io 108/114
Extensive Documentation
docs.puli.io
Slide 108
Slide 108 text
Bernhard Schussek · webmozart.io 109/114
Successfully in Production
Slide 109
Slide 109 text
Bernhard Schussek · webmozart.io 110/114
Working Symfony Bundle
puli/symfony-bundle
Slide 110
Slide 110 text
Bernhard Schussek · webmozart.io 111/114
Symfony Distribution
puli/symfony-puli-edition
Bernhard Schussek · webmozart.io 114/114
Get Involved!
github.com/container-interop/service-provider
github.com/puli/issues
Slide 113
Slide 113 text
Bernhard Schussek · webmozart.io 115/114
@webmozart
Slide 114
Slide 114 text
Bernhard Schussek · webmozart.io 117/114
Questions?
Questions?
Bernhard Schussek
Bernhard Schussek
@webmozart
@webmozart
https://www.flickr.com/photos/puliarfanita/7167723539 (CC BY 2.0)
webmozart.io