Ejemplo n.º 1
0
 /**
  * Install Webservice from site
  *
  * @param   string  $client      Client
  * @param   string  $webservice  Webservice Name
  * @param   string  $version     Webservice version
  * @param   string  $path        Path to webservice files
  * @param   int     $id          Path to webservice files
  *
  * @return  boolean  Returns id if Webservice was successfully installed
  */
 public function installWebservice($client = '', $webservice = '', $version = '1.0.0', $path = '', $id = 0)
 {
     $webserviceXml = RApiHalHelper::getWebservices($client, $webservice, $version, $path, true);
     if (!empty($webserviceXml)) {
         $operations = array();
         $scopes = array();
         $client = RApiHalHelper::getWebserviceClient($webserviceXml);
         $version = !empty($webserviceXml->config->version) ? (string) $webserviceXml->config->version : $version;
         if (!empty($webserviceXml->operations)) {
             foreach ($webserviceXml->operations as $operation) {
                 foreach ($operation as $key => $method) {
                     if ($key == 'task') {
                         foreach ($method as $taskKey => $task) {
                             $displayName = !empty($task['displayName']) ? (string) $task['displayName'] : $key . ' ' . $taskKey;
                             $scopes[] = array('scope' => strtolower($client . '.' . $webservice . '.' . $key . '.' . $taskKey), 'scopeDisplayName' => ucfirst($displayName));
                         }
                     } else {
                         $operations[] = strtoupper(str_replace(array('read', 'create', 'update'), array('GET', 'PUT', 'POST'), $key));
                         $displayName = !empty($method['displayName']) ? (string) $method['displayName'] : $key;
                         $scopes[] = array('scope' => strtolower($client . '.' . $webservice . '.' . $key), 'scopeDisplayName' => ucfirst($displayName));
                     }
                 }
             }
         }
         RApiHalHelper::$installedWebservices[$client][$webservice][$version] = array('name' => $webservice, 'version' => $version, 'title' => (string) $webserviceXml->name, 'path' => (string) $path, 'xmlFile' => $client . '.' . $webservice . '.' . $version . '.xml', 'xmlHashed' => md5($webserviceXml), 'operations' => json_encode($operations), 'scopes' => json_encode($scopes), 'client' => $client, 'state' => 1, 'id' => $id);
         /** @var RedcoreTableWebservice $table */
         $table = RTable::getInstance('Webservice', 'RedcoreTable');
         $table->bind(RApiHalHelper::$installedWebservices[$client][$webservice][$version]);
         // Check the data.
         if (!$table->check()) {
             return false;
         }
         if (!$table->store()) {
             if (empty($id)) {
                 $this->setError(JText::sprintf('COM_REDCORE_WEBSERVICES_WEBSERVICE_NOT_INSTALLED', $table->getError()));
             }
             return false;
         }
         RApiHalHelper::saveOAuth2Scopes($client, $webservice, $scopes, false);
         $this->setState($this->getName() . '.id', $table->id);
         return $table->id;
     }
     return false;
 }