1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 
<?php

declare(strict_types=1);

namespace Wtf;

class App extends \Slim\App
{
    /**
     * Override Slim constructor to add some magic.
     *
     * @param mixed $container
     */
    public function __construct($container = [])
    {
        /*
         * Try to find config_dir in passed config,
         * because it's required for config loading.
         * If not passed, use default `pwd`/config
         */
        if (!isset($container['config_dir'])) {
            $container['config_dir'] = \getcwd().'/config';
        }

        // To register dependencies, we need container object, not array
        if (\is_array($container)) {
            $container = new \Slim\Container($container);
        }

        // Register system dependencies
        $container->register(new Provider());

        // If application has custom service provider, register it
        foreach ($container['config']('suit.providers', []) as $provider) {
            $container->register(new $provider());
        }

        $this->add($container['globalrequest_middleware']);
        foreach ($container['config']('suit.middlewares', []) as $middleware) {
            $this->add($container[$middleware]);
        }

        // Merge slim default settings with user defined
        $settings = \array_merge((array) $container['settings']->all(), $container['config']('suit.settings', []));
        unset($container['settings']);
        $container['settings'] = $settings;

        // And, finally, run Slim constructor
        parent::__construct($container);
    }

    /**
     * {@inheritdoc}
     */
    public function run($silent = false)
    {
        //Allow routing on application level via invokable class
        $this->getContainer()['app_router']($this);

        return parent::run($silent);
    }
}