Renaming the Package0:00
Okay, let's get back to it. A number of things I want to do in this lesson. First and foremost, we want to get this package up on Packages.org so we can pull it in for any project we're working on. But before we do that, a number of things I want to do. Now, right now we were calling this package Dolly, kind of a crappy name. Somebody in the comments section had the idea to use the actual Russian name, which would be Matryoshka, and that's the actual Russian term for these stackable dolls. So people won't even know how to spell it. I think it's great. So why don't we change Dolly to that? So I will begin by changing Dolly to Matryoshka. Okay, let's go in there. Open this up in our editor. And a couple of things first. Let's go to composer.json. We're going to rename this Matryoshka. And then down here, let's do the same thing. So our new namespace will be that. Okay, next, we're going to have
Let's go to composer.json. We're going to rename this Matryoshka. And then down here, let's do the same thing. So our new namespace will be that. Okay, next, we're going to have to update each of the namespaces. So I'm going to try to do this pretty quickly. So let's do a global search for Larikass Dolly. Okay, and this represents all the locations where it's stored. So very quickly, why don't we do a search in here for Larikass Dolly, and I will replace that with Larikass Matryoshka. Okay, let's do our final achieve replace and update all of the files. Excellent. Okay, so we've updated that and really any location where we reference this should be updated now. Yeah, even here. Okay, great. Next step, we didn't flesh out this section right here. So if you remember, we decided this little Blade directive or whatever
Wiring ServiceProvider Dependencies1:33
should be updated now. Yeah, even here. Okay, great. Next step, we didn't flesh out this section right here. So if you remember, we decided this little Blade directive or whatever we want to name it, that's going to serve as sort of like our controller of sorts. So that needs a dependency of our RussianCaching class. And then our RussianCaching class needs an instance of this CacheRepositoryContract. So here's what we can do to tackle that. We know we'll need our RussianDollCaching. So I will do that up. Next, we know that's going to need the dependency for cache. And finally, I can say cache equals app(). And I happen to know it's cache()->store(). But we could also just reference the contract entirely. So for example, if we wanted to yank all of that, and paste it in, that would achieve the same thing. So basically, what that will do is new up the proper instance and also inject any dependencies that that class
wanted to yank all of that, and paste it in, that would achieve the same thing. So basically, what that will do is new up the proper instance and also inject any dependencies that that class might have. Alright, so we set up our cache, we set up our custom RussianCaching class and pass that in. And then finally, we can new up our Blade directive and pass that in. Make sense? Okay. Now, if we wanted to refactor this, we could even do bladeDirective, and then do new RussianCaching. And then finally, app()->make('cache.store'). And that would achieve the exact same thing, I believe. Now remember, your ServiceProvider, that's sort of like the the bootstrap for your package. So as soon as the user adds this ServiceProvider to their projects application.php configuration file, yeah, Laravel is just going to swoop through all the providers, and then register each one. So think of this section right here as our way of sort of injecting this component into Laravel so that it
yeah, Laravel is just going to swoop through all the providers, and then register each one. So think of this section right here as our way of sort of injecting this component into Laravel so that it can be used. Okay, now, honestly, I have no doubt that we'll probably need to make a number of tweaks. But for now, let's try this out locally in a real project, just to make sure everything's working the way we would hope. So real quick, I'm going to do a git status and everything and say, honestly, at this stage, when I'm still tinkering with things, I might even just say, working on package, you know, later, once it's pushed up to GitHub, and it's available to the world, you can be much more specific. But yeah, when you're still toying around with it, I honestly don't worry too much about commit messages. I only do that once the world can see it. Okay, so let's do this. Let's go back to the original project we were working on. We called it
Cleaning the Test Project3:50
I honestly don't worry too much about commit messages. I only do that once the world can see it. Okay, so let's do this. Let's go back to the original project we were working on. We called it RussianDollCachingLesson. Yeah, this is what we were working on in the initial episodes where we were seeing if we could get it to work. So now we're going to replace that with our package and see if it still does the same thing. And yeah, if we take a look, here's what we have. So we still have some of those old files that we were working with. And in fact, in our AppServiceProvider, we still have the Blade directives here. Okay, so what we're going to do is clear all of this stuff out. Okay, so let's see what else. Let's come back up, we no longer need our RussianCaching class. We don't need Cacheable. Whatever this YOLO.php file was probably from a test, we don't need that. Next, if we go into our middleware section, we should be able to get rid of Yeah, that flush
class. We don't need cacheable. Whatever this YOLO file was probably from a test, we don't need that. Next, if we go into our middleware section, we should be able to get rid of Yeah, that flush view file. So yeah, let's get rid of that. Okay, so now at this point, nothing should really work, right? And finally, by the way, I'm going to get rid of that middleware. But yeah, we should have reset everything at this point. Finally, just one more thing. Sorry about that. We're going to get rid of the notes and then in the card, get rid of that trait as well, since it no longer exists. So that means if we view this project in the browser, we're exactly where we left off. I think this was at the beginning of episode three of the series. But now we can pull in the package to see if it does the trick. Now how do we do this? This can be kind of tricky at times. So do we have to push it up to GitHub and then create the package with packages.org and then install it through
Installing Package Locally5:17
if it does the trick. Now how do we do this? This can be kind of tricky at times. So do we have to push it up to GitHub and then create the package with packages.org and then install it through there? Yeah, you could do that if you want. But then every time you make a change, it's kind of awkward, right? So instead, let's just pull in a local VCS package like this. We're going to visit our composer.json file. And now right up here, let's set up some custom repositories. Now specifically, we're only going to have one here, but the type will be VCS, and that stands for version control system. So we're just going to pull in a git repository, essentially. Now the URL to where this package is located, well, let's find it. Let's go into our code, open source, and then we called it matryoshka, and I'll get the path, the present working directory for that path. Okay, so I'm going to paste that in. And that's it. So now take a look at this.
and then we called it matryoshka, and I'll get the path, the present working directory for that path. Okay, so I'm going to paste that in. And that's it. So now take a look at this. We can require this package just like anything else. It's very neat. So I can say laracast/matryoshka, and I'll just grab DevMaster for now. Sweet. So now I can do composer update, and that will pull in our local package. And there we go, installing laracast/matryoshka. So if I switch back to my editor, and let's just close all of this, what will be the steps for the user to install this package? Well, they add it to their composer.json file. Then, and this is standard stuff, they go to their config/app.php file, and they add, if we scroll up a ways, right down here, basically to our providers array, we're going to add a new custom provider. And remember, Laravel will filter through all of the items in this array and register them.
right down here, basically to our providers array, we're going to add a new custom provider. And remember, Laravel will filter through all of the items in this array and register them. And that's how each package or component basically bootstraps itself with the framework. So let's do that now. Laracast matryoshka. And actually, it just occurred to me, I'm not sure if we changed the file name. Let's see. Does it still exist? Yeah, we forgot to change that. Okay, so that's one thing on our to-do list, but it shouldn't affect anything for now, so I'll keep it like that. And now let's come back to Chrome and see how we're doing. So if this was installed properly, this cache directive should be registered with Blade. Refresh. And actually, we need to clear our view cache, right? Don't forget that. So here I'm logged into my VM, I can do, why don't we just do both in one sweep, php artisan view:clear, and then also clear out all of
Testing Cache Behavior7:39
actually, we need to clear our view cache, right? Don't forget that. So here I'm logged into my VM, I can do, why don't we just do both in one sweep, php artisan view:clear, and then also clear out all of any old cache we might have with memcached. Okay, sorry about that. So give it a refresh. And now it looks like it's not working, but I think it actually is, because it's trying to find a getCacheKey. But we removed that Cacheable trait, so this should be expected. I only have to return it. So we have a custom trait for that, use Laracasts\Matryoshka\Cacheable. And then I can say use Cacheable exactly the way we did before. Okay, so let's go to our Card, and we're going to do the same thing here. Use Cacheable. Okay, back to Chrome, give it a refresh. And you know what? It seems like it's working. Why don't we test it out? Maybe go to an IndividualNote. And like we did before, let's just make it a big H1. Okay, so
give it a refresh. And you know what? It seems like it's working. Why don't we test it out? Maybe go to a individual note. And like we did before, let's just make it a big H1. Okay, so if we give this a refresh, they should not be heading ones here. Do you remember why? Well, it's because the first time we cached all of this. So now on subsequent page loads, we don't have to render it anymore. We can just pull directly from the cache, which means if we did it right, we won't even read or process this partial. Okay, refresh, and it worked. So one more time, why don't we boot up php artisan tinker, update one of the records, and make sure that it does refresh everything. I will say notes equals, I don't know, app()->make('notes'), and then grab the very first one. Okay, so the one beginning with this word right here. We're going to update this section. Okay, noteBody equals. Our package is working.
Renaming ServiceProvider File9:15
first, notes, and then grab the very first one. Okay, so the one beginning with this word right here. We're going to update this section. Okay, note body =. Our package is working. note save. That will save. It will update its timestamp, and then it will touch the parent, which is the card. The card's timestamp will update as well, and that should clear all the necessary cache. So if we give this a refresh, one more update should still be a list item, but this should now be in H1. Refresh, and it works. Excellent. So really, I'm just seeing the one fix we need to make. If I return to, let's go back to open source/Matyroshka and bring this back. Yeah, the only thing I see that we need to do is to update this file name. So I will rename this to MatryoshkaServiceProvider. And then finally, let's update this as well. Matryoshka. All right, so let's do a git status.
So I will rename this to MatryoshkaServiceProvider. And then finally, let's update this as well. Matryoshka. All right, so let's do a git status. Yep, we've updated both of those. So I will commit with rename service provider. Okay, so it's pretty cool that our package is working. The next step is to get this up on packages.org. So I know we said we would do that in this lesson, but this video is running a bit long, so that will be our first step in the very next video.
