Collection of common utility classes
- Run:
composer install --optimize-autoloader
Helper class over the php-di libary.
Get class instance with all dependencies injected (autowired).
$myClass = Container::get(MyClass::class);
$myClass->myMethod();
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',
]);
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');
Parser for ini files.
Example of config.ini
file:
[database]
; this is a comment
name = "my_db"
host = "localhost"
[settings]
data[] = "1"
data[] = "2"
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];
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 beginSymfony\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 packageOutputInterface
, fromSymfony\Component\Console\Output
$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),
];
[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.
Helper class that can calculate and print some script execution statistics:
- execution time (in seconds)
- memory peak usage (in MB)
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);
});
Execution time: 0.7749 seconds
Memory peak usage: 6.00 MB