private function updateApi(Module $model, ModuleSchema $module, $actions) { $repo = $this->service->getResourceRepository(); $filename = sprintf('/packages/%s/api.json', $model->getName()); if (!$repo->contains($filename)) { return; } // delete every api existent for the given module prior to create the new ones ApiQuery::create()->filterByActionId(array_values($actions))->delete(); // $extensions = $this->service->getExtensionRegistry()->getExtensionsByPackage('keeko.api', $model->getName()); $json = Json::decode($repo->get($filename)->getBody()); $swagger = new Swagger($json); foreach ($swagger->getPaths() as $path) { /* @var $path Path */ foreach (Swagger::$METHODS as $method) { if ($path->hasOperation($method)) { $op = $path->getOperation($method); $actionName = $op->getOperationId(); if (!isset($actions[$actionName])) { continue; } // find required parameters $required = []; foreach ($op->getParameters() as $param) { /* @var $param Parameter */ if ($param->getIn() == 'path' && $param->getRequired()) { $required[] = $param->getName(); } } // $prefix = isset($extensions[$actionName]) // ? $extensions[$actionName] // : $module->getSlug(); $prefix = $module->getSlug(); $fullPath = str_replace('//', '/', $prefix . '/' . $path->getPath()); $api = new Api(); $api->setMethod($method); $api->setRoute($fullPath); $api->setActionId($actions[$actionName]); $api->setRequiredParams(implode(',', $required)); $api->save(); } } } $model->setApi(true); $model->save(); }
/** * Loads the given action * * @param Action|string $actionName * @param string $format the response type (e.g. html, json, ...) * @return AbstractAction */ public function loadAction($nameOrAction, $format = null) { $model = null; if ($nameOrAction instanceof Action) { $model = $nameOrAction; $actionName = $nameOrAction->getName(); } else { $actionName = $nameOrAction; } if (!isset($this->actions[$actionName])) { throw new ModuleException(sprintf('Action (%s) not found in Module (%s)', $actionName, $this->model->getName())); } if ($model === null) { $model = $this->actions[$actionName]['model']; } /* @var $action ActionSchema */ $action = $this->actions[$actionName]['action']; // check permission if (!$this->service->getFirewall()->hasActionPermission($model)) { throw new PermissionDeniedException(sprintf('Can\'t access Action (%s) in Module (%s)', $actionName, $this->model->getName())); } // check if a response is given if ($format !== null) { if (!$action->hasResponder($format)) { throw new ModuleException(sprintf('No Responder (%s) given for Action (%s) in Module (%s)', $format, $actionName, $this->model->getName())); } $responseClass = $action->getResponder($format); if (!class_exists($responseClass)) { throw new ModuleException(sprintf('Responder (%s) not found in Module (%s)', $responseClass, $this->model->getName())); } $responder = new $responseClass($this); } else { $responder = new NullResponder($this); } // gets the action class $className = $model->getClassName(); if (!class_exists($className)) { throw new ModuleException(sprintf('Action (%s) not found in Module (%s)', $className, $this->model->getName())); } $class = new $className($model, $this, $responder); // locales // ------------ $localeService = $this->getServiceContainer()->getLocaleService(); // load module l10n $file = sprintf('/%s/locales/{locale}/translations.json', $this->package->getFullName()); $localeService->loadLocaleFile($file, $class->getCanonicalName()); // // load additional l10n files // foreach ($action->getL10n() as $file) { // $file = sprintf('/%s/locales/{locale}/%s', $this->package->getFullName(), $file); // $localeService->loadLocaleFile($file, $class->getCanonicalName()); // } // // load action l10n // $file = sprintf('/%s/locales/{locale}/actions/%s', $this->package->getFullName(), $actionName); // $localeService->loadLocaleFile($file, $class->getCanonicalName()); // assets // ------------ $app = $this->getServiceContainer()->getKernel()->getApplication(); $page = $app->getPage(); // scripts foreach ($action->getScripts() as $script) { $page->addScript($script); } // styles foreach ($action->getStyles() as $style) { $page->addStyle($style); } return $class; }