در حال بارگذاری ...

Introducing Symfony Finder0:00

The Finder component is easily one of Symfony's most intuitive offerings. So take a look at this nice syntax. Finder. We can look for any files in a directory. We can limit it to a file type. We can limit it to when the file was created. We can even filter it down to only the files that contain the given text. Really nice and really easy to use. Let me show you. composer require symfony/finder.

Installing and Basic Setup0:21

Let me show you. composer require symfony/finder. Now if I switch over to Sublime, let's play with it here. So the first step is to import the Finder component. I'll just use a little macro here. This is what you want. Symfony\Component\Finder\Finder, Finder. Okay, so let's create a new Finder instance. And we want all files within... How about to start the application directory here?

And we want all files within... How about to start the application directory here? Okay, so we can use app_path for that. That's just a Laravel helper function. Okay, so we accept files. And then let's say for each files as file, we're just going to var_dump the file's realPath. So getRealPath. All right, let's take a look at this in the browser. Now, yeah, it's a little hard to read.

All right, let's take a look at this in the browser. Now, yeah, it's a little hard to read. But hopefully you can see for each file that was matched, we can see the full file path. Very useful. Okay, so we can see that alone is incredibly useful. If you ever want to say, give me all files within a particular directory, that's great. And it's a lot easier than glob or scan_directory.

Filtering by Filename1:19

that's great. And it's a lot easier than glob or scanDirectory. I don't know about you. I always have to go back and refer to how to use those. But now let's say we want to limit this search to only those of a certain file type. So as you know, most of these files are .php. But I've also littered just a couple dummy files here. bar.txt and then also a foo.txt. Okay, so we only want those guys.

Bar.txt and then also a foo.txt. Okay, so we only want those guys. So we just say named star.txt. And that's it. So here's what I like to do. I like to put each call on its own line. To make it just a little more clear. So if I give this a refresh, now we see two files. Bar.txt and foo.txt. But now I want to take it a step further.

Filtering by File Contents2:00

Bar.txt and foo.txt. But now I want to take it a step further. Maybe I'm only interested in the text files that contain a particular sequence of characters. So here I'm using this Idea. Maybe it's like a template or something you're going to search and replace. So example key is in this file as well as that file. Let's add one more. App another.txt.

Let's add one more. app/another.txt. And I'll just add some lorem text. Okay, so what we want to say is, right now you're going to pick up all three files. However, we're only interested in the ones that contain this little template here. Okay, let's say give me all files named star.txt, but also only the files that contain that sequence of characters.

Updating Matched Files2:41

but also only the files that contain that sequence of characters. Okay, so now we should just get two. And we do. We're back to bar and foo. So now, yeah, you can do whatever you want with this. For example, we could filter through the files and change them. So let's say contents equals file_get_contents(file_get_real_path).

So let's say $contents equals file_get_contents(file), get_file(file), get_real_path(file). So now, yeah, if I var_dump(that), that's going to give you the contents of each file there. There's one. There's two. Then you could do a replacement. So str_replace('example', 'updated', $string).

We're going to look for example key. Change it to updated. And then finally base that upon the contents here. All right. So now, let's just write to it file_put_contents to file_get_contents realpath. And put the updated contents. And that would do the trick. So if we run it again, and we now take a look at the files,

So if we run it again, and we now take a look at the files, we've updated that one and that one. But once again, another.txt did not contain that sequence. So it was ignored entirely. So think about that. Think how really 10 lines. We can even clean this up a little bit. If we want to add a web in it, we could turn it into a collection,

Finder Uses in Laravel4:12

And also only the ones that contain this given set of characters. Yeah, on your own, it's kind of a headache. Now there's more to the API. You can do a lot of stuff like exclude file types or set the depth level and lots of cool stuff. But I want to finish up this lesson by just showing you all the different places or some of the places in Laravel where this sort of code might be used.

or some of the places in Laravel where this sort of code might be used. Okay, so why don't we find in this folder, finder, create and see what we get. Okay, how about this one right here? This is Eloquent's factory class. So if you've ever used database factories to quickly whip up records for testing or even database seeding, this is how we do it. So let's take a look.

or even database seeding, this is how we do it. So let's take a look. We notice it's instantiated with an instance of Faker. And then in the constructor, we get the path to the factories folder. So that would be this folder right here. And then we call a load method on it. So if we take a look at that, there we go. So finder, create, give me all the files within the database/factories folder.

So finder, create, give me all the files within the database/factories folder. Okay, so this tells you something. It is not only reading model factory, it's going to read every single file within that directory. So you may not have known that, but now you do. So it fetches every factory file. And then for each one, it requires it in. And then there, each one can register itself.

And then for each one, it requires it in. And then there, each one can register itself with the factory. And by the way, that factory variable within here, that comes from this instance here. Okay, so that's one example. What else? What else might be cool? Okay, how about this? This is where we load configuration.

Okay, how about this? This is where we load configuration. So this is where Laravel reads the config files. Once again, we use it. So we get the path to the config folder. So that's just going to be basically base_path() /config. And then we say within that configuration directory, give me all .php files. And then once again, for each one, we will process them. Okay, maybe one more.

And then once again, for each one, we will process them. Okay, maybe one more. Here we go. Okay, php artisan app:name. So this is where if you create a new application and you say php artisan app:name, we'll do Laracast. What that's going to do is a bunch of stuff. One, it'll update all your namespaces. So for example, let's go to AuthController. And you'll see that before it was a default of app,

So for example, let's go to AuthController. And you'll see that before it was a default of app, but after it's Laracast. So this is good if you do want your root namespace to be something unique to your business. Really, in practice, I've been fine keeping it to app, even though I thought I wouldn't, but I don't care. It's fine. Anyways, pretty cool stuff. If you've ever wondered how that works, this is it.

Anyways, pretty cool stuff. If you've ever wondered how that works, this is it. So once again, finder. So we're going to look for all *.php files within the project. And we're going to replace the namespace. Okay, so notice, replace namespace, and we're passing through a path to the file. Let's see what that looks like. Okay, so within each file, we're looking for namespace app, basically.

Okay, so within each file, we're looking for namespace app, basically. So this namespace app, we're looking for that. But now we're going to replace it with namespace Laracast. Okay, so we call replace. And there we go. So fetch the file from the finder search, perform the search and replace, and then write to that updated file. So this is just like we were doing with the facade.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟