Refactoring Into Compiler0:00
(upbeat music) Okay, so we have the bare minimum working. We've seen our machine replace a tag like X button with this hyphen-haven button thing. Let's make it a little more mature. Let's add some scaffolding around it and let's actually get this thing to run something that is real, okay? So rather than doing this all in a callback 'cause the nesting's gonna get too deep,
So rather than doing this all in a callback 'cause the nesting's gonna get too deep, it's gonna get messy, I'm gonna create a new class called compiler. Did you know that you can make classes right next to other classes inside PHP? I'm sure it breaks PSR2 and isn't a good idea, but I do it sometimes like at the bottom of test files. So class compiler and let's reference that right in here instead so we can just immediately refer
So class compiler and let's reference that right in here instead so we can just immediately refer to this new compiler or defer to it and we'll run a method called compile and we'll pass the input like that and we'll return that through. So now we can create a new function called compile. It's a method, but I didn't write public because you technically don't have to in PHP. It assumes it's public by default.
because you technically don't have to in PHP. It assumes it's public by default. We're being cowboys, this is fun. We're not using an LLM today. We're actually writing code. Why not have some fun, right? We take some input in like this, okay? And then we could return all of this. Yeah, let's yank this out, okay? ♪ Beep, beep, beep, beep, beep, beep, beep, doo, doo, doo ♪
Extracting Pattern and Match1:14
Yeah, let's yank this out, okay? ♪ Beep, beep, beep, beep, beep, beep, beep, doo, doo, doo ♪ Okay. And then let's extract this into some nice stuff. So let's say protected pattern equals and then we'll take this pattern like here and we'll put it there. We got a nice property like that. Pretty nice, right? This pattern, okay?
Pretty nice, right? This pattern, okay? And let's pull this out a little bit into like three separate thingies, okay? Like this. I don't know if that's any better. And then this function here, like rather than having this deep callback, I wanna put this into its own method here, like this function match.
I wanna put this into its own method here, like this function match. We'll call it match. And then I guess we can pull in matches or I think like groups is a better name because it's really capture groups. You always see matches, but there's really, with Preg Replace callback, it's gonna call this for every match. So this is like matches within matches,
it's gonna call this for every match. So this is like matches within matches, which I think is silly. This should be called groups. We're gonna call it groups, okay? We'll put this down here, do, do, do. We'll get groups, do, do, do, groups name, we get the name, we return that, okay? Now we can just call it like this, right? But I'm gonna show you a fun little trick.
Method Callback Syntax2:20
Now we can just call it like this, right? But I'm gonna show you a fun little trick. In PHP 8.2 or three or something or one, I have no idea. You can reference a method as a callback with this triple dot syntax. So now this basically provides a callback or a closure or whatever that is this method already bound to the this context and everything. So this is really nice for like pulling chain type closures
to the this context and everything. So this is really nice for like pulling chain type closures and such into like flat hierarchies like this. Just pull them up as dedicated, well named methods like match. I think it's perfect, okay? So look at how clean this is. We got pattern, we got compile, we got match and we're piping it all through that right up here and we haven't tried any of it.
Testing the Refactor2:58
and we're piping it all through that right up here and we haven't tried any of it. So let's hit save and see if it works. It worked, it works, okay? We don't know it works because it's the same exact thing but we can test it by adding a hyphen and we save and there we go, it added the hyphen, perfect. So let's do something useful. Now everything we're doing that matters is down here inside match, right?
Generating Require Output3:17
Now everything we're doing that matters is down here inside match, right? So we have the name. Now I think what we should do here is we want our output to be just something like that pure like require statement where we just require the source file directly for now, okay? So let's do that. We'll start at the end I guess is we're gonna return like a PHP type thing. You guys use here docs syntax?
like a PHP type thing. You guys use here docs syntax? I like this, you know, here docs syntax and PHP for a few reasons but one of them is you can write like when I'm writing HTML you can actually call it HTML and you can write like something like this and usually I don't know why that didn't work. Yeah, usually your editor will syntax highlight it which is really nice where if we use PHP will it syntax highlight it?
which is really nice where if we use PHP will it syntax highlight it? No, it doesn't, okay. Anyway, so we're gonna return some PHP. It's gonna be open PHP tag require and then we need some kind of path. We'll build that up in a variable up here, okay? And then here, okay. So that's what we wanna do. So now we wanna convert this name into the path
So that's what we wanna do. So now we wanna convert this name into the path and for I guess we can just like require this blade file directly. That's not gonna fly. We're gonna need to actually compile that because we're getting away with it because the source of this button file like here button.blade doesn't have blade in it at all. It's just HTML so we can require it directly.
like here button.blade doesn't have blade in it at all. It's just HTML so we can require it directly. So that's what we'll do just to demonstrate things and get the ball rolling. We'll say path equals resource path and then inside here, we say views components slash and then let's turn this into these slash and then we wanna say name.blade.php. Okay, let's DD path just so we're on the same page here. If I refresh, it's gonna show you.
Okay, let's DD path just so we're on the same page here. If I refresh, it's gonna show you. Ah, you're seeing actual files in my computer. That is my name, dead giveaway, you got me. Kayla Porsio, I'm doxed. Resources views components button.blade.php, perfect. So now we can just put that into a require statement like this. If I save, we'll see that down here, it should have changed to our thing but I bet I made a mistake and it's throwing an error.
it should have changed to our thing but I bet I made a mistake and it's throwing an error. Yeah, let's see, what's that error? Unexpected token forward slash. Oh, I know, we're not putting this in quotes at all. Gotta put it in quotes, okay? Now we save it. This thing's maybe still broken, I don't know, let's try again. Is it still broken? What's our problem?
Is it still broken? What's our problem? I rendered a button. Why isn't this showing anything? Oh, because it has unsaved changes. So I have to close it. Oh, this is what a thing. This is usually where you stop the video and then you edit it out but not us, not us. We're doing it live.
and then you edit it out but not us, not us. We're doing it live. All right, we have the require just like that as we want and now it works, isn't that kinda cool? It works. We have X button up here. We made our machine, intercept it, learn the name, reference an actual blade file for it, require that file instead and it works. If we make a bunch of them like this, we load the page.
Need Blade Compilation6:13
require that file instead and it works. If we make a bunch of them like this, we load the page. Haha, pretty cool, huh? I don't know, I think that's pretty cool. So yeah, we did that but this is not gonna fly because let's just show you why it's not gonna fly. As soon as I try to do something like echo hay or something and then I save it, okay, watch this. Yeah, it didn't work, of course, because this is not PHP. We need to actually still compile that component
Yeah, it didn't work, of course, because this is not PHP. We need to actually still compile that component from blade into PHP and then require that compiled file down here, that's gonna be a whole fun adventure. So let's do it.
