2017 © Pedro Peláez
 

composer-plugin slim-composer-installer

image

mc388/slim-composer-installer

  • Wednesday, July 20, 2016
  • by mc388
  • Repository
  • 3 Watchers
  • 3 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Slim Composer Installer

This module registered all your Slim 3 modules and load the dependencies., (*1)

Usage

Slim 3 project

Add the new composer package to your composer.json file, (*2)

composer require mc388/slim-composer-installer

Create an App class src/App.php to your Slim 3 project, (*3)

<?php

namespace SlimTest;

use Mc388\SlimComposerInstaller\Autoloader;

/**
 * Class App initialize the slim app
 * @package App
 */
class App extends \Slim\App
{

    /**
     * App constructor.
     */
    public function __construct()
    {
        parent::__construct([]);
        $this->loadModules();
    }

    /**
     * Load all installed slim-modules
     */
    private function loadModules()
    {
        $autoloader = new Autoloader();

        // Get all modules
        foreach ($autoloader->getModules() as $module) {
            // Iterate over each module namespace
            foreach ($module['namespaces'] as $namespace) {
                // Build class name
                $moduleClass = $namespace . 'App';

                // Instantiate module class
                new $moduleClass($this);
            }
        }
    }
}

The public/index.php file should looks like this:, (*4)

<?php

use SlimTest\App;

session_start();

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

$app = new App();
$app->run();

Slim Module

There are a few requirements to your Slim 3 module to use this package. The composer package type is slim-module and the autoloader/psr-4 attribute is set., (*5)

Here is an example for the composer.json file, (*6)

{
  "name": "mc388/slim-test",
  "type": "slim-module",
  "autoload": {
    "psr-4": {
      "Mc388\\SlimTest\\": "src"
    }
  },
  "require": {
    "mc388/slim-composer-installer": "v1.0.0"
  }
}

Put all your dependencies to a src/App.php file, for example:, (*7)

<?php

namespace Mc388\SlimTestModule;

use Interop\Container\ContainerInterface;
use Slim\App as SlimApp;

class App
{
    /**
     * App constructor.
     * @param SlimApp $app
     */
    public function __construct(SlimApp $app)
    {
        /** @var ContainerInterface $container */
        $container = $app->getContainer();

        $container['view'] = function (ContainerInterface $container) {
            // Init twig
        };

        $app->get('/test', function ($request, $response) {
            return 'Hello World!';
        });
    }
}

After installing the module, the route /test should be available, (*8)

The Versions

20/07 2016

dev-develop

dev-develop

  Sources   Download

MIT

The Requires

  • composer-plugin-api ^1.0

 

20/07 2016

dev-scrutinizer-patch-1

dev-scrutinizer-patch-1

  Sources   Download

MIT

The Requires

  • composer-plugin-api ^1.0

 

11/07 2016

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

  • composer-plugin-api ^1.0

 

11/07 2016

v1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires

  • composer-plugin-api ^1.0

 

05/07 2016

v0.2.0

0.2.0.0

  Sources   Download

MIT

The Requires

  • composer-plugin-api ^1.0

 

04/07 2016

v0.1.0

0.1.0.0

  Sources   Download

MIT

The Requires

  • composer-plugin-api ^1.0