Skip to content

Separated services default with trait #292

Description

@sayonara90s

Custom config services:

<?php namespace Config;

class Services
{
    use \CodeIgniter\Services;

    public static function myCustom($getShared = true)
    {
        if ($getShared)
        {
            return self::getSharedInstance('autoloader');
        }

        return new App\Libraries\MyCustom();
    }

}

Default CodeIgniter:

<?php namespace CodeIgniter;

trait Services
{
    static protected $instances = [];

    // More CodeIgniter default config services...

    protected static function getSharedInstance(string $key, ...$params)
    {
        if (! isset(static::$instances[$key]))
        {
            // Make sure $getShared is false
            array_push($params, false);

            static::$instances[$key] = self::$key(...$params);
        }

        return static::$instances[$key];
    }

    public static function __callStatic(string $name, array $arguments)
    {
        $name = strtolower($name);

        if (method_exists(__CLASS__, $name))
        {
            return Services::$name(...$arguments);
        }
    }
}

I think that is very clean. What do you think, Sir?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions