WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.046 --> 00:00:02.036 align:middle
I have a secret.

00:00:02.456 --> 00:00:05.996 align:middle
When our project was created,
it wasn't 15 files.

00:00:06.396 --> 00:00:08.406 align:middle
It was... one file.

00:00:08.926 --> 00:00:11.506 align:middle
If you peeked inside the code
for the symfony new command,

00:00:11.846 --> 00:00:14.826 align:middle
you'd discover that it's a
shortcut for just two things.

00:00:15.226 --> 00:00:19.296 align:middle
First, it clones a repository
called symfony/skeleton...

00:00:19.486 --> 00:00:22.876 align:middle
which is just one file if
you ignore the license.

00:00:23.196 --> 00:00:25.736 align:middle
And second, it runs composer install.

00:00:26.156 --> 00:00:26.606 align:middle
That's it!

00:00:26.996 --> 00:00:32.796 align:middle
But hold on, if that's the case, where in the
world did all these other files come from?

00:00:33.046 --> 00:00:35.896 align:middle
Like, the stuff in bin/, config/ and src/?

00:00:36.576 --> 00:00:42.686 align:middle
The answer starts with a special package inside
our composer.json file called symfony/flex.

00:00:43.236 --> 00:00:50.026 align:middle
Flex is a Composer plugin that adds two
superpowers to Composer: aliases and recipes.

00:00:50.666 --> 00:00:51.906 align:middle
Aliases are simple.

00:00:52.506 --> 00:00:55.786 align:middle
To add a new package to your app
- which we'll do in a minute -

00:00:56.146 --> 00:01:02.056 align:middle
you run composer require then the name
of the package like symfony/http-client.

00:01:02.636 --> 00:01:04.556 align:middle
Flex gives the most important packages

00:01:04.556 --> 00:01:08.206 align:middle
in the Symfony ecosystem a
shorter name, called an alias.

00:01:08.696 --> 00:01:14.536 align:middle
For example, symfony/http-client
has an alias called http-client.

00:01:15.056 --> 00:01:18.396 align:middle
Yup, we could run composer require http-client

00:01:18.596 --> 00:01:21.796 align:middle
and Flex would translate that
to the final package name.

00:01:22.296 --> 00:01:24.726 align:middle
It's just a shortcut when adding packages.

00:01:25.316 --> 00:01:30.736 align:middle
If you want to see all the available aliases,
go to a repository called symfony/recipes...

00:01:30.886 --> 00:01:33.056 align:middle
then click the link to RECIPES.md.

00:01:33.866 --> 00:01:36.026 align:middle
On the right, there they are!

00:01:36.726 --> 00:01:41.226 align:middle
The second superpower that Symfony
Flex adds to Composer is recipes.

00:01:41.466 --> 00:01:43.156 align:middle
These are fascinating.

00:01:43.656 --> 00:01:49.426 align:middle
When you add a new package, it may have a
recipe, which is basically a set of files

00:01:49.426 --> 00:01:51.036 align:middle
that will be added to your project.

00:01:51.536 --> 00:01:57.286 align:middle
And it turns out that every file that we
started with - in bin/, config/, public/ -

00:01:57.656 --> 00:02:02.106 align:middle
these all came from the recipes of the
packages that were originally installed.

00:02:02.756 --> 00:02:08.056 align:middle
For example, symfony/framework-bundle is
the "core" package of the Symfony Framework.

00:02:08.706 --> 00:02:14.136 align:middle
You can check out its recipe by going to the
symfony/recipes repository and navigating

00:02:14.136 --> 00:02:19.396 align:middle
to symfony, framework-bundle,
then the latest version.

00:02:20.306 --> 00:02:26.856 align:middle
Boom! Check out config/packages/: most of the
stuff we started with came from this recipe!

00:02:27.816 --> 00:02:30.296 align:middle
Another way to see the recipes
is at your command line.

00:02:30.716 --> 00:02:36.726 align:middle
Run: composer recipes Apparently the recipes
of four different packages were installed.

00:02:37.256 --> 00:02:41.466 align:middle
And we could get info about any of these by
adding its name to the end of the command.

00:02:42.216 --> 00:02:46.206 align:middle
Anyway, recipes are amazing
because we can install a package

00:02:46.246 --> 00:02:48.676 align:middle
and instantly get any files we need.

00:02:49.216 --> 00:02:52.676 align:middle
Instead of fussing around with
configuration, we get right to work.

00:02:53.406 --> 00:02:58.666 align:middle
Let's try this out: let's add a
new package called PHP-CS-Fixer

00:02:58.956 --> 00:03:02.876 align:middle
that will give us an executable
file to fix the styling of our code.

00:03:03.776 --> 00:03:06.696 align:middle
For example, in
src/Controller/MainController.php,

00:03:06.696 --> 00:03:10.856 align:middle
if you follow PHP coding standards,
the curly brace should live

00:03:10.856 --> 00:03:13.056 align:middle
on the next line after a function.

00:03:13.656 --> 00:03:18.236 align:middle
If we did something like this, our
file now violates those standards.

00:03:18.666 --> 00:03:22.656 align:middle
That wouldn't hurt anything, but you know,
we want to keep our code looking clean.

00:03:23.146 --> 00:03:25.546 align:middle
And PHP-CS-Fixer can help us do that.

00:03:26.086 --> 00:03:33.416 align:middle
To install it, run: composer require
cs-fixer-shim And yes, this is an alias.

00:03:33.896 --> 00:03:38.316 align:middle
On top, the true package is php-cs-fixer/shim.

00:03:39.016 --> 00:03:41.086 align:middle
Did this package come with a recipe?

00:03:41.416 --> 00:03:46.846 align:middle
It did! The Configuring php-cs-fixer/shim
tells us that.

00:03:47.256 --> 00:03:52.346 align:middle
But, we can also see it by running:
git status The fact that composer.json

00:03:52.346 --> 00:03:57.866 align:middle
and composer.lock are modified
is 100% normal Composer behavior.

00:03:58.376 --> 00:04:02.626 align:middle
You can see that composer.json has
the new library under the require key.

00:04:03.406 --> 00:04:07.896 align:middle
But every other modified or new file
is thanks to the package's recipe.

00:04:08.476 --> 00:04:09.936 align:middle
Let's investigate these!

00:04:10.456 --> 00:04:11.826 align:middle
Open up .gitignore.

00:04:12.806 --> 00:04:18.126 align:middle
Cool! At the bottom, it added two new
entries for two common files that you want

00:04:18.126 --> 00:04:20.716 align:middle
to ignore when you use PHP CS fixer.

00:04:21.326 --> 00:04:26.056 align:middle
The recipe also added a new
.php-cs-fixer.dist.php file.

00:04:26.496 --> 00:04:29.226 align:middle
This is CS Fixer's configuration file.

00:04:29.636 --> 00:04:30.316 align:middle
And check it out!

00:04:30.566 --> 00:04:32.976 align:middle
It's pre-built to work for our Symfony app.

00:04:33.366 --> 00:04:37.696 align:middle
It tells it to fix all files in the current
directory, but ignore the var/ directory

00:04:37.886 --> 00:04:40.406 align:middle
because that's where Symfony
stores its cache files.

00:04:41.056 --> 00:04:44.026 align:middle
It also tells it to use a
ruleset called Symfony.

00:04:44.536 --> 00:04:48.096 align:middle
That means that we want our code
style to match Symfony's style.

00:04:48.686 --> 00:04:52.766 align:middle
The point is: instead of us wasting
time hunting down this default config...

00:04:53.116 --> 00:04:54.006 align:middle
we just get it!

00:04:54.856 --> 00:04:57.396 align:middle
The last modified file is symfony.lock.

00:04:57.726 --> 00:05:01.976 align:middle
This keeps track of which recipes we
have installed and at what version.

00:05:02.436 --> 00:05:06.346 align:middle
And yes, we are going to commit
all these files to our repository.

00:05:07.146 --> 00:05:09.506 align:middle
Now that we've installed
the package, let's use it.

00:05:10.036 --> 00:05:10.826 align:middle
Do that by running: .

00:05:11.126 --> 00:05:16.156 align:middle
/vendor/bin/php-cs-fixer That'll
show all the available commands.

00:05:16.426 --> 00:05:18.526 align:middle
The one we want is called fix.

00:05:18.946 --> 00:05:20.396 align:middle
Try it: And...

00:05:20.756 --> 00:05:24.206 align:middle
yes! It found the violation
in MainController.php!

00:05:24.956 --> 00:05:26.406 align:middle
When we go to that file...

00:05:26.956 --> 00:05:31.826 align:middle
yea! It moved my curly brace from the end
of the line back down to the next line.

00:05:32.316 --> 00:05:33.376 align:middle
That's awesome.

00:05:34.246 --> 00:05:37.976 align:middle
Next up, let's meet and install
one of my favorite libraries in all

00:05:37.976 --> 00:05:40.466 align:middle
of PHP: the Twig templating engine.

