Run a “Hello World” using Saeghe

Morteza
6 min readOct 30, 2022

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.

Attention needed

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:

Make an empty application

There is no file in this project yet. Let’s run the init command on Saeghe to initialize the project.

running saeghe init command

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.

contents of the saeghe.config.json 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.

saeghe.config-lock.json after running the init command

Now let’s make an index.php file as the entry point of our application.

an empty index.php file

We need to define this file as an entry point on Saeghe.

define entry point in saeghe.config.json

Like any other application, let’s start printing a “Hello World”.

echo hello world from index.php

Now to be able to run this application, we need to build it using Saeghe.

project’s structure after the build

As you can see, there is a newbuilds 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 index.php file.

index.php contents after the build

Let’s run the application

output of the index.php file

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 saeghe.config.json file.

define a map in saeghe.config.json

Now I’m going to create the controller:

WelcomeController file

Let’s remove the echo from the index.php file and use this controller instead.

use WelcomeController in the index.php

That’s it for now. Let’s build and run it.

build and run the index.php

Great, it works! Now let’s take a look at the newly built index.php file.

built index.php file contains the controller

Saeghe added the controller path to the spl_autoload_register function. This way, PHP can find required classes by running this command. There is anotherspl_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 builtWelcomeController file, there is no change compared to the main file.

built WelcomeController 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

add CLI package using the add command

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 saeghe.config.json file:

saeghe.config.json after adding the CLI package

As you see, the package’s URL and its installed version got added to the saeghe.config.json file.

There are also some changes in the saeghe.config-lock.json file:

saeghe.config-lock.json file after adding the CLI package

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 thePackages/saeghe/cli/saeghe.config.json file, you will see the test-runner is defined under the packages section.

CLI’s saeghe.config.json file

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.

WelcomeController using CLI\line function

Again, let’s build and run the application.

run the application using the line function

You will see the same output in the output.

Now let’s take a look at the built files. First the index.php :

built index.php file after adding the CLI package

The only change in the index.php file happened in the namespaces mapspl_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-mapspl_autoload_register.

Now let’s take a look at the built controller:

built controller after adding the CLI package

There is an added require_once statement to the Write.php file in the CLI package that contains the line function we used.

Sum up

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.

--

--