slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.94k stars 1.95k forks source link

Slim container final (again) #1666

Closed mbretter closed 8 years ago

mbretter commented 8 years ago

It's related to https://github.com/slimphp/Slim/issues/1652

I would like to extend \Slim\Container just for putting my @property-read phpdoc, so that IDEs code completion works properly.

any ideas?

silentworks commented 8 years ago

Implement your own and override the Slim\Container.

silentworks commented 8 years ago

Please don't open multiple issues on the same topic.

mbretter commented 8 years ago

copying all of the Slim\Container code just for adding some php doc, really?

silentworks commented 8 years ago

If you need them yes.

irfanevrens commented 8 years ago

@mbretter I started use league/container as container. Using this service provider class. You can customize it what you need.

<?php namespace App\ServiceProviders;

use League\Container\ServiceProvider\AbstractServiceProvider;
use Slim\CallableResolver;
use Slim\Collection;
use Slim\Handlers\Error;
use Slim\Handlers\NotAllowed;
use Slim\Handlers\NotFound;
use Slim\Handlers\Strategies\RequestResponse;
use Slim\Http\Environment;
use Slim\Http\Headers;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Router;

class SlimServiceProvider extends AbstractServiceProvider
{
    /**
     * @var array
     */
    private $settings = [
        'httpVersion'                       => '1.1',
        'responseChunkSize'                 => 4096,
        'outputBuffering'                   => 'append',
        'determineRouteBeforeAppMiddleware' => false,
        'displayErrorDetails'               => false
    ];

    /**
     * @param array $settings
     */
    public function __construct(array $settings = [])
    {
        $this->settings = array_merge($this->settings, $settings);
    }

    /**
     * @var array
     */
    protected $provides = [
        'settings',
        'environment',
        'request',
        'response',
        'router',
        'foundHandler',
        'errorHandler',
        'notFoundHandler',
        'notAllowedHandler',
        'callableResolver'
    ];

    /**
     * Use the register method to register items with the container via the
     * protected $this->container property or the `getContainer` method
     * from the ContainerAwareTrait.
     *
     * @return void
     */
    public function register()
    {
        $this->container->share('settings', function() {
            return new Collection($this->settings);
        });

        $this->container->share('environment', function() {
            return new Environment($_SERVER);
        });

        $this->container->share('request', function() {
            return Request::createFromEnvironment($this->container->get('environment'));
        });

        $this->container->share('response', function() {
            $headers = new Headers(['Content-Type' => 'text/html; charset=UTF-8']);
            $response = new Response(200, $headers);

            return $response->withProtocolVersion($this->container->get('settings')['httpVersion']);
        });

        $this->container->share('router', function() {
            return new Router();
        });

        $this->container->share('foundHandler', function() {
            return new RequestResponse();
        });

        $this->container->share('errorHandler', function() {
            return new Error($this->container->get('settings')['displayErrorDetails']);
        });

        $this->container->share('notFoundHandler', function() {
            return new NotFound();
        });

        $this->container->share('notAllowedHandler', function() {
            return new NotAllowed();
        });

        $this->container->share('callableResolver', function() {
            return new CallableResolver($this->container);
        });
    }
}
mbretter commented 8 years ago

thx, switching to a different container implementation looks like a good idea ...