Run a “Hello World” using Saeghe
So far, I have told you about the general idea of Saeghe and also explained how Saeghe works. In this article, I will show you how to run an application using Saeghe.
If you want to do it also by yourself, make sure you installed Saeghe by continuing the documentation here.
Saeghe is a difficult name to pronounce. Therefore, the Saeghe project has been renamed to phpkg.
Please visit phpkg website at phpkg.com
Step 1: Setup the application
Let’s start by creating an empty project. The project’s name will be
hello-world and I’m going to open it in the PhpStorm IDE.
If you want to follow these steps, run
mkdir hello-world and open the directory in your IDE of choice.
So far, I have this:
There is no file in this project yet. Let’s run the
init command on Saeghe to initialize the project.
After running the
init command, I have two files and one directory. A
Packages directory. This directory will store installed packages; for now, it is an empty directory.
There is a
saeghe.config.json file that is the project’s configuration file.
There is another file name
saeghe.config-lock.json. Saeghe will use it to keep the required metadata for package management. For now, there is an empty
packages object in this file.
Now let’s make an
index.php file as the entry point of our application.
We need to define this file as an entry point on Saeghe.
Like any other application, let’s start printing a “Hello World”.
Now to be able to run this application, we need to build it using Saeghe.
As you can see, there is a new
builds directory. The
buildsdirectory has a subdirectory name
development. There is a copy of the main files and directories under this directory. You can also build the project for the
production environment by running
saeghe build production . For more information, please check the build documentation.
The only difference is in the contents of the
Let’s run the application
Congratulation! You just ran your first built application.
Now let’s make it more complicated.
Step 2: Add classes
Let’s add a controller class and use the controller to print the output. For this, we first need to define our project source in Saeghe.
I’m going to use the
app directory as my source directory. Feel free to use any other name. Make sure you put your directory name in the
Now I’m going to create the controller:
Let’s remove the
echo from the
index.php file and use this controller instead.
That’s it for now. Let’s build and run it.
Great, it works! Now let’s take a look at the newly built
Saeghe added the controller path to the
spl_autoload_register function. This way, PHP can find required classes by running this command. There is another
spl_autoload_register defined as a fallback mechanism that will get used if the first one can not return the required file.
If we look at the built
WelcomeController file, there is no change compared to the main file.
So now you know how Saeghe uses classes. Let’s go further.
Step 3: Use packages and functions
Now I’m going to use a package for printing the output, instead of using the
echo command directly. For this case, I’m going to use the Saeghe CLI package using its git URL which is https://github.com/saeghe/cli.git. We need to use the
add command on Saeghe to add this package by running:
saeghe add https://github.com/saeghe/cli.git
Saeghe added the CLI package under our
Packages directory. There is also another package installed. The
test-runner package is a package used in the CLI package. As you saw, we used the git URL directly to add the CLI package.
Now let’s take a look at our
As you see, the package’s URL and its installed version got added to the
There are also some changes in the
Here you see there are two packages added in this file. The CLI package that we asked Saeghe to add and the
test-runner package that is a package used in the CLI package.
If you take a look at the
Packages/saeghe/cli/saeghe.config.json file, you will see the
test-runner is defined under the
Let’s use the CLI package in our application. The CLI package has some functions to work in CLI mode. You can check the list of available functions in its documentation. For this application, I’m going to use the
line function that is going to write the given string in the output.
Again, let’s build and run the application.
You will see the same output in the output.
Now let’s take a look at the built files. First the
The only change in the
index.php file happened in the namespaces map
spl_autoload_register. As you see, there are 3 more defined namespaces compared to the previous build. These namespaces come from installed package configs. Since we didn’t use any new classes, there is no change in the class-map
Now let’s take a look at the built controller:
There is an added
require_once statement to the
Write.php file in the CLI package that contains the
line function we used.
Here you saw some samples of how you can use Saeghe and how it works. You can follow the getting started documentation and discover more by yourself.
I hope you got excited about it. I believe there is a lot of cool stuff we can do using Saeghe. The ability to apply namespaced functions and having separate environments for developing and executing the code can open new doors to much cool stuff.
I’m looking forward to seeing you give it a try and join me create a wonderful community around it.