Exemple #1
0
 /**
  * Compute an HTTP POST method.
  *
  * @param  Request   $request     HTTP request.
  * @param  Response  $response    HTTP response.
  * @return bool
  * @throws Exception\Dav\Exception
  */
 function httpPost(Request $request, Response $response)
 {
     if (System\Collection::NAME . '/' . Node::NAME !== $request->getPath()) {
         return;
     }
     $payload = @json_decode($request->getBodyAsString());
     if (!$payload || !isset($payload->transport) || !isset($payload->username) || !isset($payload->password)) {
         throw new Exception\Dav\Exception('Payload is corrupted.');
     }
     $this->configuration->mail = new StdClass();
     $this->configuration->mail->transport = $payload->transport;
     $this->configuration->mail->username = $payload->username;
     $this->configuration->mail->password = $payload->password;
     $this->configuration->save();
     $response->setHeader('Content-Type', 'application/json');
     $response->setBody(json_encode(true));
     return false;
 }
 function case_save()
 {
     $this->given($file = $this->helper->configuration('configuration.json', ['a' => 42]), $initialContent = file_get_contents($file), $configuration = new CUT($file))->when($result = $configuration->save())->then->boolean($result)->isTrue()->string(file_get_contents($file))->isEqualTo($initialContent)->when($configuration->a = 153, $result = $configuration->save())->then->boolean($result)->isTrue()->string(file_get_contents($file))->isNotEqualTo($initialContent)->given($configuration = new CUT($file))->when($result = $configuration->a)->then->integer($result)->isEqualTo(153);
 }
 /**
  * Create the configuration file.
  * The content must be of the form:
  *     [
  *         'baseUrl'  => …,
  *         'database' => [
  *             'driver'   => …,
  *             'host'     => …, // if MySQL
  *             'port'     => …, // if MySQL
  *             'name'     => …, // if MySQL
  *             'username' => …,
  *             'password' => …
  *         ]
  *     ]
  * The configuration file will be saved before being returned.
  *
  * @param  string  $filename    Filename of the configuration file.
  * @param  array   $content     Configurations.
  * @return Configuration
  * @throw  Exception\Installation
  */
 static function createConfigurationFile($filename, array $content)
 {
     if (!isset($content['baseUrl']) || !isset($content['database']) || empty($content['database']['driver']) || !isset($content['database']['username']) || !isset($content['database']['password'])) {
         throw new Exception\Installation('Configuration content is corrupted. Expect at least ' . 'a base URL, a database driver, username and password.', 5);
     }
     if ('mysql' === $content['database']['driver'] && (empty($content['database']['host']) || empty($content['database']['port']) || empty($content['database']['name']))) {
         throw new Exception\Installation('Configuration content is corrupted for MySQL. Expect ' . 'at least a host, a port and a name.', 6);
     }
     if (false === static::checkBaseUrl($content['baseUrl'])) {
         throw new Exception\Installation('Base URL is not well-formed, given %s.', 6, $content['baseUrl']);
     }
     switch ($content['database']['driver']) {
         case 'mysql':
             $dsn = sprintf('mysql:host=%s;port=%d;dbname=%s', $content['database']['host'], $content['database']['port'], $content['database']['name']);
             break;
         case 'sqlite':
             $dsn = sprintf('sqlite:%s_%d.sqlite', SABRE_KATANA_PREFIX . '/data/database/katana', time());
             break;
         default:
             throw new Exception\Installation('Unknown database %s.', 7, $content['database']['driver']);
     }
     touch($filename);
     $configuration = new Configuration($filename, true);
     $configuration->base_url = $content['baseUrl'];
     $configuration->database = new StdClass();
     $configuration->database->dsn = $dsn;
     $configuration->database->username = $content['database']['username'];
     $configuration->database->password = $content['database']['password'];
     $configuration->save();
     return $configuration;
 }