Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @namespace
- */
- namespace Application\Service\Api\Protocol;
- use Application\Service\Api as ApiService;
- /**
- * Api Handler BasicProtocol
- *
- * @author miholeus
- */
- class Basic extends AbstractProtocol
- {
- /**
- *
- * @param string $action service and method name separated by "." symbol
- * @param array $params method params
- * @return mixed
- * @throws \Application\Service\Api\HandlerException
- * @throws \Application\Service\Api\Service\Exception
- */
- public function execute($action, $params)
- {
- $actionArr = explode(".", $action);
- $serviceName = $actionArr[0];
- // set default service action
- if(!isset($actionArr[1])) {
- $serviceAction = "version";
- } else {
- $serviceAction = $actionArr[1];
- }
- $className = '\\Application\\Service\\Api\\Service\\' . ucfirst($serviceName);
- if(!class_exists($className)) {
- throw new ApiService\HandlerException("Unknown service " . $serviceName);
- }
- /** @var $service \Application\Service\Api\Service\AbstractService */
- $service = new $className($this);
- if(!$service instanceof ApiService\ServiceInterface) {
- throw new ApiService\HandlerException("Service " . $serviceName . " should implement"
- . " \\Application\\Service\\Api\\ServiceInterface");
- }
- if(!method_exists($service, $serviceAction)) {
- throw new ApiService\HandlerException(
- sprintf("Unknown method %s in service %s", $serviceAction, $serviceName)
- );
- }
- $this->service = $service;
- $this->service->setParams($params);
- return call_user_func_array(array($service, $serviceAction), array($params));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment