DEV Community

Cover image for Bootstrapping a CLI PHP application in Vanilla PHP

Bootstrapping a CLI PHP application in Vanilla PHP

Erika Heidi on September 20, 2019

Introduction PHP is well known for its popularity with web applications and CMSs, but what many people don't know is that PHP is also a ...
Collapse
 
syntaxseed profile image
SyntaxSeed (Sherri W) • Edited

Following this on Termux on my phone & can't get Composer to work.

So here's an autoloader for anyone who needs to do this without composer:

(Include this file into the minicli file.)

<?php
/**
 * Example project-specific auto-loading implementation.
 *
 * After registering this autoload function with SPL, the namespaces on the left of the loaders array will load classes found in the paths on the right.
 *
 * @param string $class The fully-qualified class name.
 * @return void
 */

 spl_autoload_register(function ($class) {
    $loaders = [
        'Minicli\\' => '/lib/',
        'App\\' => '/app/'
    ];

foreach($loaders as $prefix => $base_dir){
    $len = strlen($prefix);
    if(strncmp($prefix, $class,$len) !== 0){
        continue;
    }
    $relative_class = substr($class, $len);

    $file = __DIR__ . $base_dir .
       str_replace('\\', '/',
         $relative_class) .
       '.php';

    if (file_exists($file)) {
        require $file;
        return;
    }
} //end foreach
});
Enter fullscreen mode Exit fullscreen mode
Collapse
 
_ezell_ profile image
Ezell Frazier

PHP as a general purpose tool? I'm intrigued.

Collapse
 
q2dg profile image
Osqui LittleRiver

Well, it isn't useful for building native graphical applications, for instance. Also it has some problems to manage serial port (I use Arduino boards a lot). So...about this aspect Php is far behind Python, I think

Collapse
 
erikaheidi profile image
Erika Heidi

That's precisely why the post uses the therm CLI, because it's for the command line only: scripts. Surely, PHP can't solve all problems, but it is a very low barrier language that many people are already familiar with. I said all these things in the article's introduction by the way

Collapse
 
nueaf profile image
Michael Als

Excellent article. Thank you!

Collapse
 
rescatado182 profile image
Diego Pinzón

Great tutorial Erika, it's really very helpful for my daily working. I just wanna mention that I've worked on Windows 10 and xampp, so, if you get those too, for running on a console, yo need, for example: #!C:\xampp\php\php.exe.

BTW, vim is my least favorite editor, hahahahaha!!

Collapse
 
jodyshop profile image
Waleed Barakat

Nice, but what is the output of this in real life, is there is a demo or something to try?

Thanks

Collapse
 
riccycastro profile image
Ricardo Castro

Well, you can do a lot with this, if you use to use Symfony or Laravel, for sure, at some point you die use the cli to generate new controller, entities/models, services, database and the tables... Your imagination is the limit

Collapse
 
erikaheidi profile image
Erika Heidi

Hi! The idea is that we build together a little framework for creating PHP applications in the command line, we'll refactor a few things in the next posts of this series, but the way it is now it has two example commands that you can use as base to build something else. It's very versatile. You can find this code here: erikaheidi/minicli:0.1.0.

Collapse
 
syntaxseed profile image
SyntaxSeed (Sherri W)

Following along using Termux on my phone, while camping. 😊

Collapse
 
erikaheidi profile image
Erika Heidi

that's AMAZING!

Collapse
 
anwar_nairi profile image
Anwar

I always found PHP appealing for CLI, your article demonstrate it! Very cool 😉 Looking forward the next parts!

Collapse
 
erikaheidi profile image
Erika Heidi

Thank you! :-)

Collapse
 
tabraizbukhari profile image
Tabraizbukhari

Great

Collapse
 
silverman42 profile image
Sylvester Nkeze

This is really awesome.

Collapse
 
erikaheidi profile image
Erika Heidi

Thank you!

Collapse
 
mateuschmitz profile image
Mateus Schmitz

Great article. Thanks!

Collapse
 
doctor_brown profile image
James Miranda

Great tutorial! Thank you for it.

Collapse
 
kod7dev profile image
KodSeven.Dev

p-e-r-f-e-c-t

Collapse
 
erikaheidi profile image
Erika Heidi

Thank you 🤗

Collapse
 
coopz profile image
Leonard Cooper

except that we are not working with a front end anymore - yay!
Erica Heidi circa 2019

Bravo! Words so sweet, bringing a tear of pure joy down my cheek. BRAVO