Okay, guys, let's create our script that will load the database schema.
So first let me jump to the bootstrap file because this one is incomplete.
We are having an auto loader here.
We are declaring the strict types and loading the config.
But we haven't really used our dependency injection container yet.
So I'd like to use the core app.
Let me add this use statement somewhere over here.
And now I'd like to bind to this dependency injection container.
First I'd like to get the configuration.
That's something we are loading right up here.
And another thing that I'd like to bind is the database.
So essentially those resources whether that's config or our created object will be available under those
names.
Now here we are going to create a new new instance of this database class.
Let's make sure we have a use statement for it.
So this is core database.
And the database requires a config.
That's why we're going to get the config database.
So we've got some type issues here.
Let's check this out.
Okay so apparently I forgot to add a namespace.
Okay.
So let's add the core namespace.
There we have it.
Everything is fine.
Now our bootstrap file is complete.
We can use the database in both the HTTP server.
So our application and inside the command line scripts.
Now let's go ahead and create one.
So I can close this bootstrap file and all the scripts.
We're going to put them inside the bin folder as some binary scripts.
Let me create the first one.
I'm going to call this schema underscore load.
PHP add a PHP tag.
And the first thing we're going to do is we require once the bootstrap file.
So we're getting the current directory.
And I think we need to go one level up and get the bootstrap file.
Okay.
So we should have a dependency injection container here.
And since the container uses static methods I can grab the database by just using the app get method.
And the name is database.
So at this point I'm just connected to database.
I don't need to do anything extra.
This gives me a database.
The one slight problem that we have is that this returns a mixed type.
So later on, when we're gonna use the methods of the database object of the database class, we won't
get any suggestions inside the editor or the IDE.
And we are going to solve this problem a little later.
For now we need to manage somehow.
Now let me move this require line at the top of the script.
All right.
So the logic here is pretty simple.
We need to figure out the schema file.
So this is also the current directory.
And I'm going to go up and grab from the database folder I think this is called schema SQL.
We might make this configurable, but I think it's just good enough for now.
Okay, so this is database schema SQL.
That should be fine.
And the actual SQL to run we're going to use file getcontents.
This will just load the contents of the schema file.
So the SQL to run.
And at this point we're going to run it.
Let's wrap everything with a try catch block.
And here we're just going to be catching a generic exception.
So if anything goes wrong we're gonna say error loading schema maybe.
We're gonna also output the message.
This is not for our users.
That's always for us.
So I think it's safe to just output that.
Okay.
I think this error handling is enough.
Let's add the concatenation character and the logic goes inside this try block.
So let's take a look at the schema again.
This is just a series of create table statements that as you can see are separated by semicolon.
Okay let's use that.
Maybe we can use explode.
Let's see.
Let me call that parts.
And let's use PHP explode function where the separator is semicolon and the string is SQL.
Notice I'm using named parameters for a built in PHP function.
And at this point let's just see what those parts are and stop okay, now I can open the command line
and I'm going to run PHP bin schema load PHP.
Okay.
So we've got some issues inside the database.
PHP file.
Okay.
So we are missing the PDO class.
Let's make sure it's imported here.
So we need to use the PDO class.
When I'm just typing the PDO exception I just got a suggestion to import those classes.
I think yes almost everything.
We also need the PDO statement.
So I did that to have this list of imports.
Exception pdo pdo exception pdo statement I think that should be everything, at least from that file.
Okay, let's try running this again.
So this is our SQL but we've got four items and we are creating three tables, not four.
Let's take a look.
So that's an SQL.
This is an SQL.
This looks like an SQL.
And then the fourth element with index three is an empty string.
Well we don't want that that's for sure.
So let's wrap that with an array filter without any additional parameters.
It will just check if every single item is not empty.
If you skip this callback.
So we are skipping the callback.
And I expect to get three items right now.
And that's the case.
We've got an array with three SQL statements creating the tables.
Okay, why don't we run it one by One.
Let's do the for each loop and go with parts as part or call it SQL part.
And now the db.
The database object has a query method to which we pass the SQL part.
So we run it one by one.
Obviously we need to get rid of this line and if everything goes fine we're just going to say schema
loaded successfully.
And a new line.
Let's run it again.
Mm.
Apparently everything was loaded.
So now I can open it using this browser inside Visual Studio Code.
Well, we didn't have to create this file.
I can see we've got those tables with all the columns that we have defined, which means that this script
has worked fine.
So even though the script is fine, let's make a further improvement.
And for now, let me delete this database, the blog SQLite file like that and let me open the composer.json.
So using composer, you can also define some custom scripts that will make it a little bit easier to
run your own command line interface commands.
And also it will help everyone using your commands.
Guess the proper name.
You're gonna see that in action in a second.
So we can add this scripts section where you're gonna give your command names like schema load.
That's the command name and it will run PHP bin forward slash.
This is load schema.
Schema load.
That's schema underscore load PHP.
Right now I can just write composer schema.
And as you see this command is not found.
But it is suggesting that I run schema load okay.
So let me run composer schema load.
There it is schema loaded successfully and obviously all the tables are there.
So we're going to use this.
We're going to use this composer scripts section to make it much easier to run all of the command line
scripts that we're going to create for our PHP apps.