Modules Provider

💫 Modules provider is an object autoloader for automating and simplifying some code inclusion in different projects. You can call object(s) from different folders and create a container with multiple objects.

Installation

composer require mediashare/modules-provider
1

Basic Usage

Class Autoloading

<?php
// ./index.php
require "vendor/autoload.php";
use Mediashare\ModulesProvider\Config;
use Mediashare\ModulesProvider\Modules;

$config = new Config();
$config->setModulesDir(__DIR__.'/modules/');
$config->setNamespace("Mediashare\\Modules\\");
$modules = new Modules($config);
1
2
3
4
5
6
7
8
9
10

Modules

Requirements

  • The classname must match the name of the php file & unique.
  • A public run() function must be present if you want to be able to execute an automation action on several modules. This can be used when you want to orchestrate a set of events via php classes.
<?php
// ./modules/Hello.php
namespace Mediashare\Modules;

class Hello
{
    public $message;
    public function run() {
        if (empty($this->message)):
            $this->message = "Not message recorded :(";
        endif;
        echo $this->message;
        return $this;
    }

    public function setMessage(string $message) {
        $this->message = $message;
        return $this;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Execute

// ./index.php
require "vendor/autoload.php";
use Mediashare\ModulesProvider\Config;
use Mediashare\ModulesProvider\Modules;

$config = new Config();
$config->setModulesDir(__DIR__.'/modules/');
$config->setNamespace("Mediashare\\Modules\\");
$modules = new Modules($config);

$hello = $modules->get("Hello");
$hello->setMessage("Hello World!\n");
$hello->run();
1
2
3
4
5
6
7
8
9
10
11
12
13
Last Updated: 1/3/2020, 4:00:09 AM