PHP Lite Framework

Overview

plite

PHP Lite Framework

Adds some basic libraries for working with console-viewed logging, PHP as a CLI tool, handling basic server-side web API requests, a very small Postgres abstraction, as well as a small framework for abstracting a few AWS services (S3, SecretsManager, and SES) and Twilio/Sinch/Plivo. Also comes with tools to help with ETL.

The AWS abstraction classes also allow you to use a local, offline version for test and dev, which does NOT require an AWS account or access to its services, but through a series of small config changes, can allow your code to immediately use cloud resources (e.g., AWS S3 instead of your local filesystem).

There are several entry points for using this library, depending on the intended usage.

Use Case: CLI

If you are using this to create a command-line script (e.g., executable with a shebang), then your starting point is the class vertwo\plite\CLI.

Just extend CLI, and implement three methods:

  • main()
  • getShortOpts()
  • getLongOpts()

The latter two methods are for unix-style options-handling can return empty string and an empty array. So, for a hello-world, just this will do:

class HelloWorld extends CLI
{
    protected function getShortOpts () { return ""; }
    protected function getLongOpts () { return []; }


    /**
     * CLI entry point.
     *
     * @return int
     */
    public function main ()
    {
        echo "Hello, world!\n";
        return 0;
    }
}


HelloWorld::run();

And, if you add a shebang, a PHP tag, a use statement, and a require statement, and then make the script executable, you'll be able to just execute this php file on the CLI (and, obviously, making sure to chmod +x or whatever your OS needs to make things runnable).

Now, obviously, that's a LOT of lines of code for a file that could have just been this:

#! /usr/bin/php
<?php
echo "Hello, world!\n";

So, why put it up with it?

Well, just 1 reason: options-handling.

In order for PHP to function well in a unix-y shell-environment, scripts often nee to be run with different arguments to have slightly different behavior.

Suppose our PHP script is named hello.

Let's say we want to add a verbose switch to our program, so that we can call it like this:

$ hello -v

Then, we just change one line:

    protected function getShortOpts () { return "v"; }

And we can use it like this:

    public function main ()
    {
        if ( $this->hasopt("v") ) echo "About to print string!\n";
        echo "Hello, world!\n";
        return 0;
    }

This makes CLI PHP much more effective.

And suppose we wanted a long option: --name your_name

Then, we change the implementation of getLongOpts() like this:

    protected function getLongOpts ()
    {
        return [
            self::REQ("name"),
        ];
    }

and use it like this:

    public function main ()
    {
        if ( $this->hasopt("v") ) echo "About to print string!";

        if ( $this->hasopt("name") )
        {
            echo "Hello, world, " . $this->getopt("name") . "!\n";
        }
        else
        {
            echo "Hello, world!\n";
        }
        return 0;
    }

Beautiful.

Here's the final, (potentially) executable script:

#! /usr/bin/php
<?php



use vertwo\plite\CLI;



require_once __DIR__ . "/../vendor/autoload.php";



class HelloWorld extends CLI
{
    protected function getShortOpts () { return "v"; }

    protected function getLongOpts ()
    {
        return [
            self::REQ("name"),
        ];
    }


    /**
     * CLI entry point.
     *
     * @return int
     */
    public function main ()
    {
        if ( $this->hasopt("v") ) echo "About to print string!\n";

        if ( $this->hasopt("name") )
        {
            echo "Hello, world, " . $this->getopt("name") . "!\n";
        }
        else
        {
            echo "Hello, world!\n";
        }
        return 0;
    }
}



HelloWorld::run();

Use Case: Logging

If you're just using this library for the logger, you just need to import one function: vertwo\plite\clog.

It uses the vertwo\plite\Log class, and the functions are loaded as a separate function into the top-level vertwo\plite namespace.

If you're wondering why it's called clog, it's shorthand for the javascript logger: console.log.

It's simple to use:

            clog("Hello, world!");

In CLI mode, clog() outputs to stderr, and uses ANSI escape sequences to colorize the output. It can either be used in 1-argument mode (printing a simple string), or, more usefully, in 2-argument mode--which prints arg 1 (the "prompt"), followed by a : , followed by arg 2 (the "value").

So, imagine our main() function looked like this:

    public function main ()
    {
        if ( $this->hasopt("v") ) echo "About to print string!\n";

        if ( $this->hasopt("name") )
        {
            echo "Hello, world, " . $this->getopt("name") . "!\n";
        }
        else
        {
            clog("Hello, world!");
            clog("Hello", "world");
        }
        return 0;
    }

Then, if no prompt is given, the output will look like this:

plite-clog-example

Beautiful.

You might also like...
A simple web application for seeing a store's books. Built with Laravel 8 (a PHP Framework).
A simple web application for seeing a store's books. Built with Laravel 8 (a PHP Framework).

HappyBookStore Happy Book Store is a simple web application for seeing a store's books. As a user, you can look what book is available in the store by

LaravelSnippets.com website | A repository of useful code snippets for Laravel PHP framework

LaravelSnippets.com website | A repository of useful code snippets for Laravel PHP framework. Submit, grab and share!

Free, open-source, online appointments platform based on Laravel PHP Framework.
Free, open-source, online appointments platform based on Laravel PHP Framework.

timegrid (Archived) Timegrid helps contractors and customers to find the perfect meeting time through online appointments. Features Built with Laravel

Self-hosted CMS platform based on the Laravel PHP Framework.
Self-hosted CMS platform based on the Laravel PHP Framework.

October is a Content Management System (CMS) and web platform whose sole purpose is to make your development workflow simple again. It was born out of

Discussion (forum) and Q&A platform. Community based on PHP Micro-Framework HLEB.
Discussion (forum) and Q&A platform. Community based on PHP Micro-Framework HLEB.

Agouti Discussion (forum) and Q&A platform. Community based on PHP Micro-Framework HLEB. Ideas We like the classification system based on labels (tags

An automated library management system developed in Laravel 4.2 PHP MVC Framework
An automated library management system developed in Laravel 4.2 PHP MVC Framework

An automated system to manage a public library. Admin panel for librarians to control and manage the system easily through an interactive interface.

A simple blog project based on a custom-created MVC framework using PHP & MySQL

A simple blog project based on a custom-created MVC framework using PHP & MySQL. That follows the Facade design pattern.

Customer Relationship Management (CRM) portal using PHP Codeigniter 4 & Tailwind CSS framework.
Customer Relationship Management (CRM) portal using PHP Codeigniter 4 & Tailwind CSS framework.

CRM Portal Customer Relationship Management (CRM) portal using PHP Codeigniter 4 & Tailwind CSS framework. Screenshots User (Dashboard) Admin (Employe

ZAP CRM is Customer Relationship Management portal built using PHP Codeigniter 4 & Tailwind CSS framework.
ZAP CRM is Customer Relationship Management portal built using PHP Codeigniter 4 & Tailwind CSS framework.

ZAP CRM ZAP CRM is Customer Relationship Management portal built using PHP Codeigniter 4 & Tailwind CSS framework. Screenshots User (Dashboard) Admin

Owner
Version2
What's Next
Version2
MOFHY Lite is a free web hosting management system to manage MOFH hosting accounts and SSL certificates.

MOFHY Lite MOFHY LITE is a priceless MyOwnFreeHost Client Area for account management, ticket support system and a free ssl service. It has easy to us

Santiago Rodríguez 6 Dec 28, 2021
While BotMan itself is framework agnostic, BotMan is also available as a bundle with the great Laravel PHP framework.

BotMan Studio About BotMan Studio While BotMan itself is framework agnostic, BotMan is also available as a bundle with the great Laravel PHP framework

BotMan 322 Dec 26, 2022
The php gRPC server framework with php-fpm and nginx.

php-grpc-server-protobuf The php grpc server framework with protobuf and DO NOT use any 3rd libraries or use Swoole. Support protobuf and json request

HeTao 5 Nov 17, 2022
A framework for building rich, data-driven applications in PHP and MySQL

Xataface A framework for building rich, data-driven applications in PHP and MySQL License GPL Requirements PHP 5.2 or higher MySQL 5 or higher Install

Steve Hannah 129 Dec 13, 2022
Open Source Point of Sale is a web based point of sale application written in PHP using CodeIgniter framework.

Open Source Point of Sale is a web based point of sale application written in PHP using CodeIgniter framework. It uses MySQL as the data back end and has a Bootstrap 3 based user interface.

opensourcepos 2.7k Jan 2, 2023
Open Source Voucher Management System is a web application for manage voucher. used PHP with Laravel Framework and use MySQL for Database.

Voucher Management System is a web application for manage voucher. You can create and manage your voucher. Voucher Management System is used PHP with Laravel Framework and use MySQL for Database.

Artha Nugraha Jonar 34 Sep 17, 2022
Web Application using MVC PHP Framework (LavaLust)

LavaLust Version 2 This is an early release of LavaLust Version 2. You may check the changelog.txt file to see the changes. Overview of Changes in Ve

Noe Dimailig 1 Oct 16, 2021
Automatic SASS-to-CSS compiling for Laravel 4 (and any other framework too), config-free, in pure PHP, works with latest SASS 3.2 .scss syntax, imports and mixins

laravel-sass Automatic Sass-to-CSS compiling for Laravel 4 (and any other framework by the way) while being in development. Every time you run your ap

Chris 71 Nov 29, 2022
Projeto Web Site do Curso Laravel Developer utilizando o Framework PHP Laravel

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Pedro Leandro 1 Oct 22, 2021
Online web application developed in PHP using Laravel framework for managing real-time kitchen orders in a restaurant.

Online web application developed in PHP using Laravel framework for managing real-time kitchen orders in a restaurant. It allows, through a web panel, real-time communication between chefs and waiters about the status of orders.

Fernando 2 Nov 9, 2022