Vestige Simple PHP Dependency Injection: Call named object functions in a queue

Recommend this page to a friend!
  Info   View files Documentation   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2021-03-30 (5 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 23 All time: 10,342 This week: 259Up
Version License PHP version Categories
vestige 1.0.0BSD License7Language, PHP 7
Description Author

This package can be use to call named object functions in a queue.

It provides a dependency injection (DI) container class that can take an associative array of objects that define the names of certain objects.

The package also allows applications to call functions of the defines objects by specifying the objects names and passing the function name and the parameters to be passed those class functions.

It also allows to queue object function calls, so the functions are called one after another like when using promises.

The package can also record the values returned by each function, so applications can retrieve the return values later.

Picture of Carlos Artur Curvelo da Matos
  Performance   Level  
Name: Carlos Artur Curvelo da ... is available for providing paid consulting. Contact Carlos Artur Curvelo da ... .
Classes: 17 packages by
Country: Portugal Portugal
Age: 43
All time rank: 323241 in Portugal Portugal
Week rank: 224 Up6 in Portugal Portugal Up
Innovation award
Innovation award
Nominee: 11x

Winner: 2x

Details

Vestige

A sophisticated word full of meaning. vestige. n. A visible trace, evidence, or sign of something that once existed but exists or appears no more. Vestige is a basic PHP experiment, based in a single class Memory. Although this class works as a 'quasi-DI', this is actually NOT its main purpose. Vestige emerges from an attempt of declaring methods from a class which hasn't yet been instantiated, and then execute those methods, once the class instance is finally created.

Even though this class has been thought as a proof of concept, a handful of possible applications come to mind: * Asynchronous class methods and tasks * Retaining results from various class methods, even after a class is destroyed * Easy to use hooks and triggers

How it works

Vestige uses the class Memory to hold class objects. Having that said, it could be a design structure just by then, but we decided to move it further. So, for understanding the process and all features, let's start by adding an array of objects to the class Memory:

use Vestige\Memory;
use Any\ClassA;
use Any\ClassB;

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

$objects = [
	'class_a' => new ClassA // Each class receives an instance tag
];

$vestige = new Memory($objects);
/*
So $vestige->objects['class_a'] contains an instance of ClassA, for instance
*/

Now, let's say ClassB has three different methods with no arguments: prepare(), toJson() and send(). Vestige makes possible to you to position those methods, even if ClassB hasn't been added to Memory or instantiated in any other way. Normally, that would throw an exception or fatal, as you can't call non-static method from a class that has no existent instances.

However, with Vestige you can leave a 'record' for each of those methods, and they will be executed in the moment a ClassB instance is created through Memory. So, let's see the code.

// ...

$vestige = new Memory($objects);

$vestige->record('class_b', 'prepare');
$vestige->record('class_b', 'toJson');
$vestige->record('class_b', 'send');

Done. The three methods are positioned and waiting for a class instance tagged as 'class_b'. If no instances like that are ever created, nothing is going to happen - not even exceptions, of course. However, if a new instance of ClassB named 'class_b' is added to Memory any time, the methods will be immediately executed.

For such, Memory is also equipped with a static method for pushing new instances to the objects array:

// ...
$vestige->record('class_b', 'prepare');
$vestige->record('class_b', 'toJson');
$vestige->record('class_b', 'send', ['mail@domain.com', 'Contact Name']);

$memory->push(['class_b' => new ClassB()]); // As an array - which means you can push several objects altogether

/*
Once the object is pushed, all queued methods are executed in sequence.
For methods with parameters, these can be passed in order, in an array,
as a third argument of the method record() - like send() example
*/

Leaving a 'vestige'

Now the process that led to this library name. So as adding new instances is possible, using push(), you can remove existent instance anytime, by using drop(). When that happens, hooked methods won't be executed anymore. Nonetheless, an interesting thing occurs. Taking the previous example, let's see what happens if we drop ClassB.

$memory->drop('class_b'); // Removes instance from $objects in Memory

// ClassB 'promised' methods are not called anymore.
// But they leave a vestige...

Well, some methods return variables or any other elements and data. If we just drop the instance, that would imply we can't get those returns anymore. Of course we could save results in a database, or even cache the serialized class and its results. But we don't wwant to cache anything, nor using a database or making any kind of query for now. But, as I promised, that first queued execution produced a special by-product: a vestige.

Even with no instances of ClassB, you can still retrieve the results, if any, for the three recorded methods. You can either get an array with the full 'vestige', or retrieving the results from one of those methods only - like this:

$one = $memory->return('class_b', 'toJson'); // Gets toJson results
$all = $memory->returnAll(); // Gets an array with all vestige data, including method results
  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file Memory.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:23
This week:0
All time:10,342
This week:259Up
For more information send a message to info at phpclasses dot org.