Giter VIP home page Giter VIP logo

common-utils's Introduction

Common-Utils project

Github Workflow Status Travis Build Status

Collection of common utility classes

Requirements

  • Minimum PHP Version
  • Composer

Installation

  • Run: composer install --optimize-autoloader

DI Container

Helper class over the php-di libary.

Usage

Container::get()

Get class instance with all dependencies injected (autowired).

$myClass = Container::get(MyClass::class);
$myClass->myMethod();
Container::make()

Useful for creating objects that should not be stored inside the container (i.e. that are not services, or that are not stateless), but that have dependencies.

It is also useful if you want to override some parameters of an object's constructor.

$myClass = Container::make(MyClass::class, [
    'user' => 'mihaitmf',
]);

Configuration

In certain situations it is helpful to use a Definitions configuration file:

  • to map interfaces to implementations
  • for classes that require primitive data types or arguments without type hint on constructor
  • to use static factory methods

Example di-config.php file:

return [
    ClientInterface::class => autowire(Client::class),
    Config::class => factory(
        function () {
            return ConfigIniParser::fromFile(__DIR__ . DIRECTORY_SEPARATOR . 'config.ini');
        }
    ),
];

Set DI configuration file in the bootstrap file, right after requiring the class loader:

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

Container::setDefinitionsFilePath(__DIR__ . DIRECTORY_SEPARATOR . 'di-config.php');

Config Parser

Parser for ini files.

Example of config.ini file:

[database]
; this is a comment
name = "my_db"
host = "localhost"

[settings]
data[] = "1"
data[] = "2"

Usage

The Parser requires the path to the config ini file and returns a Config object.

To access the config values, one can use either the object or the array access operators.

$config = ConfigIniParser::fromFile(__DIR__ . DIRECTORY_SEPARATOR . 'config.ini');

$databaseName = $config->database->name;
$databaseHost = $config['database']['host'];

$settingsData1 = $config->settings->data->{0};
$settingsData2 = $config->settings->data[1];

Command Utils

Console Event Listener

Helper class useful when building command-line scripts:

  • to print logging lines when the script execution starts and finishes
  • to print some execution statistics

It implements event handlers used with Symfony components from the symfony/console package.

The Listener has methods that can handle the following types of events:

  • Symfony\Component\Console\Event\ConsoleCommandEvent - triggered on command begin
  • Symfony\Component\Console\Event\ConsoleTerminateEvent - triggered on command finish

The methods of the Listener are intended to be used as callbacks for Symfony's EventDispatcher from the symfony/event-dispatcher package.

In order to use it, you will need to create instances for its dependencies, which are:

  • ExecutionStatistics, from the same package
  • OutputInterface, from Symfony\Component\Console\Output

Usage

$app = new Symfony\Component\Console\Application('app');

$eventDispatcher = new Symfony\Component\EventDispatcher\EventDispatcher();
$consoleEventListener = new ConsoleEventListener(
    new \CommonUtils\Command\ExecutionStatistics(),
    new \Symfony\Component\Console\Output\ConsoleOutput()
);

$eventDispatcher->addListener(
    Symfony\Component\Console\ConsoleEvents::COMMAND,
     [$consoleEventListener, 'onCommandBegin']
);
$eventDispatcher->addListener(
    Symfony\Component\Console\ConsoleEvents::TERMINATE,
    [$consoleEventListener, 'onCommandFinish']
);

$app->setDispatcher($eventDispatcher);

$app->add(new MyScriptCommand());

$app->run();

If you are using a DI library, you can include these instantiations into the DI definitions file.

Example for php-di definitions config file:

return [
    Symfony\Component\Console\Output\OutputInterface::class => autowire(Symfony\Component\Console\Output\ConsoleOutput::class),
];

Output example

[2020-11-21 02:34:35] Command <command name> started...

..... <command output> .....

Execution time: 0.7749 seconds
Memory peak usage: 6.00 MB

[2020-11-21 02:34:36] Command <command name> finished with exit code 0.

Script Execution Statistics

Helper class that can calculate and print some script execution statistics:

  • execution time (in seconds)
  • memory peak usage (in MB)

Usage

It can be wrapped around the call that needs to be analyzed:

$executionStatistics = new ExecutionStatistics();

$executionStatistics->start();
... do some work ...
$executionStatistics->end();

print($this->executionStatistics->getPrintMessage());

It can be used in the application bootstrap file to calculate the statistics for the whole application execution:

$executionStatistics = new ExecutionStatistics();
$executionStatistics->start();

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

register_shutdown_function(function () use ($executionStatistics) {
    $executionStatistics->end();
    print($this->executionStatistics->getPrintMessage());    
});

Or it can be called directly with the static method:

$startTime = microtime(true);

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

register_shutdown_function(function () use ($startTime) {
    ExecutionStatistics::printStats($startTime);    
});

Output example

Execution time: 0.7749 seconds
Memory peak usage: 6.00 MB

Enjoy!

common-utils's People

Contributors

mihaitmf avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.