public function __construct(Request $request, array $options) { parent::__construct($request, $options); $this->methods = new Map(); // create routes from db $apis = ApiQuery::create()->joinAction()->useActionQuery()->joinModule()->endUse()->find(); $routes = new RouteCollection(); /* @var $api Api */ foreach ($apis as $api) { $action = $api->getAction(); $module = $action->getModule(); $path = str_replace('//', '/', '/' . $api->getRoute()); $required = $api->getRequiredParams(); $required = is_array($required) ? explode(',', $required) : []; $name = $module->getName() . ':' . $action->getName() . '@' . $api->getMethod(); $route = new Route($path, ['path' => $path, 'action' => $action], $required, [], null, [], [$api->getMethod(), 'options']); // debug: print routes // printf("%s: %s -> %s\n", $api->getMethod(), $path, $module->getName() . ':' . $action->getName()); $routes->add($name, $route); // with params $paramRoute = clone $route; $paramRoute->setPath(sprintf('%s%s{params}', $path, $this->options['param-separator'])); $paramRoute->setRequirement('params', '.+'); $paramName = $name . 'WithParam'; $routes->add($paramName, $paramRoute); if (!$this->methods->has($path)) { $this->methods->set($path, new ArrayList()); } $this->methods->get($path)->add(strtoupper($api->getMethod())); } $this->init($routes); }
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(); }
/** * Builds a Criteria object containing the primary key for this object. * * Unlike buildCriteria() this method includes the primary key values regardless * of whether or not they have been modified. * * @throws LogicException if no primary key is defined * * @return Criteria The Criteria object containing value(s) for primary key(s). */ public function buildPkeyCriteria() { $criteria = ChildApiQuery::create(); $criteria->add(ApiTableMap::COL_ID, $this->id); return $criteria; }
/** * Internal update mechanism of Apis on Action * * @param Action $model * @param mixed $data */ protected function doUpdateApis(Action $model, $data) { // remove all relationships before ApiQuery::create()->filterByAction($model)->delete(); // add them $errors = []; foreach ($data as $entry) { if (!isset($entry['id'])) { $errors[] = 'Missing id for Api'; } else { $related = ApiQuery::create()->findOneById($entry['id']); $model->addApi($related); } } if (count($errors) > 0) { throw new ErrorsException($errors); } }
/** * Returns the number of related Api objects. * * @param Criteria $criteria * @param boolean $distinct * @param ConnectionInterface $con * @return int Count of related Api objects. * @throws PropelException */ public function countApis(Criteria $criteria = null, $distinct = false, ConnectionInterface $con = null) { $partial = $this->collApisPartial && !$this->isNew(); if (null === $this->collApis || null !== $criteria || $partial) { if ($this->isNew() && null === $this->collApis) { return 0; } if ($partial && !$criteria) { return count($this->getApis()); } $query = ChildApiQuery::create(null, $criteria); if ($distinct) { $query->distinct(); } return $query->filterByAction($this)->count($con); } return count($this->collApis); }
/** * Returns a new ChildApiQuery object. * * @param string $modelAlias The alias of a model in the query * @param Criteria $criteria Optional Criteria to build the query from * * @return ChildApiQuery */ public static function create($modelAlias = null, Criteria $criteria = null) { if ($criteria instanceof ChildApiQuery) { return $criteria; } $query = new ChildApiQuery(); if (null !== $modelAlias) { $query->setModelAlias($modelAlias); } if ($criteria instanceof Criteria) { $query->mergeWith($criteria); } return $query; }
/** * Performs an INSERT on the database, given a Api or Criteria object. * * @param mixed $criteria Criteria or Api object containing data that is used to create the INSERT statement. * @param ConnectionInterface $con the ConnectionInterface connection to use * @return mixed The new primary key. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doInsert($criteria, ConnectionInterface $con = null) { if (null === $con) { $con = Propel::getServiceContainer()->getWriteConnection(ApiTableMap::DATABASE_NAME); } if ($criteria instanceof Criteria) { $criteria = clone $criteria; // rename for clarity } else { $criteria = $criteria->buildCriteria(); // build Criteria from Api object } if ($criteria->containsKey(ApiTableMap::COL_ID) && $criteria->keyContainsValue(ApiTableMap::COL_ID)) { throw new PropelException('Cannot insert a value for auto-increment primary key (' . ApiTableMap::COL_ID . ')'); } // Set the correct dbName $query = ApiQuery::create()->mergeWith($criteria); // use transaction because $criteria could contain info // for more than one table (I guess, conceivably) return $con->transaction(function () use($con, $query) { return $query->doInsert($con); }); }
/** * Returns one Api with the given id from cache * * @param mixed $id * @return Api|null */ protected function get($id) { if ($this->pool === null) { $this->pool = new Map(); } else { if ($this->pool->has($id)) { return $this->pool->get($id); } } $model = ApiQuery::create()->findOneById($id); $this->pool->set($id, $model); return $model; }