Amfphp_Core_Common_ServiceRouter::getServiceObjectStatically PHP Метод

getServiceObjectStatically() публичный статический Метод

If none found, an exception is thrown this method is static so that it can be used also by the discovery service '__' are replaced by '/' to help the client generator support packages without messing with folders and the like the service object can either be in the global namespace or in the namespace suggested by the name. For example a call to Sub1/Sub2/NamespaceTestService will load the PHP file in Sub1/Sub2/NamespaceTestService, and return an instance of either NamespaceTestService or Sub1\Sub2\NamespaceTestService
public static getServiceObjectStatically ( type $serviceName, array $serviceFolders, array $serviceNames2ClassFindInfo ) : Object
$serviceName type
$serviceFolders array
$serviceNames2ClassFindInfo array
Результат Object service object
    public static function getServiceObjectStatically($serviceName, array $serviceFolders, array $serviceNames2ClassFindInfo)
    {
        $serviceObject = null;
        if (isset($serviceNames2ClassFindInfo[$serviceName])) {
            $classFindInfo = $serviceNames2ClassFindInfo[$serviceName];
            require_once $classFindInfo->absolutePath;
            $serviceObject = new $classFindInfo->className();
        } else {
            $temp = str_replace('.', '/', $serviceName);
            $serviceNameWithSlashes = str_replace('__', '/', $temp);
            $serviceIncludePath = $serviceNameWithSlashes . '.php';
            $exploded = explode('/', $serviceNameWithSlashes);
            $className = $exploded[count($exploded) - 1];
            //no class find info. try to look in the folders
            foreach ($serviceFolders as $folder) {
                $folderPath = NULL;
                $rootNamespace = NULL;
                if (is_array($folder)) {
                    $rootNamespace = $folder[1];
                    $folderPath = $folder[0];
                } else {
                    $folderPath = $folder;
                }
                $servicePath = $folderPath . $serviceIncludePath;
                if (file_exists($servicePath)) {
                    require_once $servicePath;
                    if ($rootNamespace == NULL) {
                        $serviceObject = new $className();
                    } else {
                        $namespacedClassName = $rootNamespace . '\\' . str_replace('/', '\\', $serviceNameWithSlashes);
                        $serviceObject = new $namespacedClassName();
                    }
                }
            }
        }
        if (!$serviceObject) {
            throw new Amfphp_Core_Exception("{$serviceName} service not found ");
        }
        return $serviceObject;
    }

Usage Example

Пример #1
0
 /**
  * does the actual collection of data about available services
  * @return array of AmfphpDiscovery_ServiceInfo
  */
 public function discover()
 {
     $availableServiceNames = $this->getAvailableServiceNames(self::$serviceFolderPaths, self::$serviceNames2ClassFindInfo);
     $ret = array();
     foreach ($availableServiceNames as $availableServiceName) {
         $serviceObject = Amfphp_Core_Common_ServiceRouter::getServiceObjectStatically($availableServiceName, self::$serviceFolderPaths, self::$serviceNames2ClassFindInfo);
         $reflectionObj = new ReflectionObject($serviceObject);
         $availablePublicMethods = $reflectionObj->getMethods(ReflectionMethod::IS_PUBLIC);
         $methods = array();
         foreach ($availablePublicMethods as $methodDescriptor) {
             $availableMethodName = $methodDescriptor->name;
             if (substr($availableMethodName, 0, 1) == '_') {
                 //methods starting with a '_' as they are reserved, so filter them out
                 continue;
             }
             $parameters = array();
             $method = $reflectionObj->getMethod($availableMethodName);
             $parameterDescriptors = $method->getParameters();
             foreach ($parameterDescriptors as $parameterDescriptor) {
                 $availableParameterName = $parameterDescriptor->name;
                 $type = '';
                 if ($parameterDescriptor->getClass()) {
                     $type = $parameterDescriptor->getClass()->name;
                 }
                 $parameterInfo = new AmfphpDiscovery_ParameterDescriptor($availableParameterName, $type);
                 $parameters[] = $parameterInfo;
             }
             $methodInfo = new AmfphpDiscovery_MethodDescriptor($availableMethodName, $parameters);
             $methods[$availableMethodName] = $methodInfo;
         }
         $serviceInfo = new AmfphpDiscovery_ServiceDescriptor($availableServiceName, $methods);
         $ret[$availableServiceName] = $serviceInfo;
     }
     //note : filtering must be done at the end, as for example excluding a Vo class needed by another creates issues
     foreach ($ret as $serviceName => $serviceObj) {
         foreach (self::$excludePaths as $excludePath) {
             if (strpos($serviceName, $excludePath) !== false) {
                 unset($ret[$serviceName]);
                 break;
             }
         }
     }
     return $ret;
 }
All Usage Examples Of Amfphp_Core_Common_ServiceRouter::getServiceObjectStatically