lib/DependencyInjector.php
public function injectIntoMethod( $object, $methodName = "injectDependencies" ){
        $ref = new \ReflectionClass($object);
        try{
            $refMethod = $ref->getMethod( $methodName );
        }catch( ReflectionException  $e ){
            return null;
        }
        
        $toInject = [];
        foreach ($refMethod->getParameters() as $p) {
            $className = $p->getClass()->name;
            if( $className === "Pimple\\Container" ){
                $toInject[] = $this->container;
            }else{
                $toInject[] = $this->container[ $className ];
            }
        }
        return $refMethod->invoke( $object, ... $toInject );
    }
public function injectIntoMethod( $object, $methodName = "injectDependencies" ){
        $ref = new \ReflectionClass($object);
        try{
            $refMethod = $ref->getMethod( $methodName );
        }catch( ReflectionException  $e ){
            return null;
        }
        
        $toInject = [];
        foreach ($refMethod->getParameters() as $p) {
            $className = $p->getClass()->name;
            if( $className === "Pimple\\Container" ){
                $toInject[] = $this->container;
            }else{
                $toInject[] = $this->container[ $className ];
            }
        }
        return $refMethod->invoke( $object, ... $toInject );
    }
public function injectIntoMethod ( $ object , $ methodName = "injectDependencies" ) { $ ref = new \ ReflectionClass ( $ object ) ; try { $ refMethod = $ ref -> getMethod ( $ methodName ) ; } catch ( ReflectionException $ e ) { return null ; } $ toInject = [ ] ; foreach ( $ refMethod -> getParameters ( ) as $ p ) { $ className = $ p -> getClass ( ) -> name ; if ( $ className === "Pimple\\Container" ) { $ toInject [ ] = $ this -> container ; } else { $ toInject [ ] = $ this -> container [ $ className ] ; } } return $ refMethod -> invoke ( $ object , ... $ toInject ) ; }
Inject dependencies from, or including, the Pimple container, into a given method on a given object
@param $object
@param $methodName
@return mixed The method's return value
Inject dependencies from or including the Pimple container into a given method on a given object