Skip to content

DI support #22

@lifeisfoo

Description

@lifeisfoo

Create another plugin base class to extend?
Code from vanilla Controller::GetImports()

     public $Uses = array('InstalledModelName');

    public function __construct()
    {
        $this->InitDI();
    }
    public function InitDI() //from Controller::GetImports
    {
        foreach ($this->Uses as $Class) {
            if(strlen($Class) >= 4 && substr_compare($Class, 'Gdn_', 0, 4) == 0) {
                $Property = substr($Class, 4);
            } else {
                $Property = $Class;
            }

            // Find the class and instantiate an instance..
            if(Gdn::FactoryExists($Property)) {
                $this->$Property = Gdn::Factory($Property);
            } if(Gdn::FactoryExists($Class)) {
                // Instantiate from the factory.
                $this->$Property = Gdn::Factory($Class);
            } elseif(class_exists($Class)) {               
                // Instantiate as an object.
                $ReflectionClass = new ReflectionClass($Class);
                // Is this class a singleton?
                if ($ReflectionClass->implementsInterface("ISingleton")) {
                    eval('$this->'.$Property.' = '.$Class.'::GetInstance();');
                } else {
                    $this->$Property = new $Class();
                }
            } else {
                trigger_error(ErrorMessage('The "'.$Class.'" class could not be found.', $this->ClassName, '__construct'), E_USER_ERROR);
            }
      }
    }

Metadata

Metadata

Assignees

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