What is Symfony *really*? It's a collection of *35* independent libraries, and
Drupal uses less than *half* of them! That means that there's a *ton* of other
good stuff that you can bring into your project to solve common problems... as
long as you know how, and what those components do!
In this talk, we'll have some fun: taking a tour of the Symfony components, how
to install them (into Drupal, or anywhere) and how to use some of my *favorite*,
lesser-known components. By the end, you'll have a better appreciation of what
Symfony *really* is, and some new tools to use immediately.
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017
1. The Coolest Symfony
Components you’ve never
heard of
with your friend @weaverryan
(they’re just hiding in your project, waiting for their big break)
2. > Lead of the Symfony documentation team
> Writer for KnpUniversity.com
> SensioLabs & Blackfire
fanboy/evangelist
> Husband of the much more
talented @leannapelham
knpuniversity.com
twitter.com/weaverryan
Hallo!
> Father to my more handsome son,
Beckett
15. public function build() {
$items = [];
$items[] = Drupal::root();
return [
'#theme' => 'item_list',
'#list_type' => 'ul',
'#items' => $items,
];
}
Do you want to build a snowman? Or a block?
29. use SymfonyComponentFinderFinder;
$finder = new Finder();
$finder->files()
->in(Drupal::root().'/core/themes/*/templates')
->name('*.twig')
->size('> 1k')
->sortByName()
;
$items = [];
foreach ($finder as $file) {
// Cool! But what is $file?
}
30. use SymfonyComponentFinderFinder;
$finder = new Finder();
$finder->files()
->in('ftp://example.com/templates/*/templates')
->name('*.twig')
->size('> 1k')
->sortByName()
;
$items = [];
foreach ($finder as $file) {
// Cool! But what is $file?
}
31. use SymfonyComponentFinderFinder;
$finder = new Finder();
$finder->files()
->in(Drupal::root().'/core/themes/*/templates')
->name('*.twig')
->size('> 1k')
->sortByName()
;
$items = [];
foreach ($finder as $file) {
// Cool! But what is $file?
}
55. $store = new RedisStore(
new PredisClient('tcp://localhost:6379')
);
$store = new RetryTillSaveStore($store);
$factory = new Factory($store);
$lock = $factory->createLock('some-lock');
if ($lock->acquire()) {
// do you stuff that needs locking!
$lock->release();
}
62. // create a new item by trying to get it from the cache
$numProducts = $cache->getItem('stats.num_products');
// assign a value to the item and save it
$numProducts->set(4711);
$cache->save($numProducts);
// retrieve the cache item
$numProducts = $cache->getItem('stats.num_products');
if (!$numProducts->isHit()) {
// ... item does not exists in the cache
}
// retrieve the value stored by the item
$total = $numProducts->get();
// remove the cache item
$cache->deleteItem('stats.num_products');
63. @weaverryan
✓choose from many adapters
(apcu, db, memcached, redis, etc)
✓cache tagging invalidation
✓expiration
✓free cookies
65. # Get the special price if
user.getGroup() in ['good_customers', 'collaborator']
# Promote article to the homepage when
article.commentCount > 100 and article.category not in ["misc"]
# Send an alert when
product.stock < 15
What if you need to write business
logic… that’s stored in the database?
67. # ABC.routing.yml file
old_bay_season_food:
path: /season/oldbay/{count}
# require there to be a ?load=1 on the URL
condition: "context.getQueryString() == 'load=1'"
services:
# new OldBaySeasoner(
# $container->get('config.factory')->get('old_bay.default')
# );
old_bay.seasoner:
class: Drupalold_bayServiceOldBaySeasoner
arguments:
- '@=service("config.factory").get("old_bay.default")'
maybe in the future???
80. Side Note:
COMPOSER <3’S YOUR INSANE
LEGACY PROJECT
@weaverryan
(that you've been forced to maintain and not allowed to modernize, despite
numerous meetings and efforts to explain things to management)