Skip to content

Commit

Permalink
Add endpoint to create new API envs
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Jun 28, 2018
1 parent cb2b5b6 commit a1a17f9
Show file tree
Hide file tree
Showing 13 changed files with 551 additions and 175 deletions.
6 changes: 4 additions & 2 deletions src/core/Directus/Console/Common/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Directus\Application\Application;
use Directus\Console\Common\Exception\SettingUpdateException;
use Directus\Util\Installation\InstallerUtils;
use Zend\Db\TableGateway\TableGateway;

class Setting
Expand All @@ -12,14 +13,15 @@ class Setting
private $db;
private $settingsTableGateway;

public function __construct($base_path)
public function __construct($base_path, $env = null)
{
if ($base_path == null) {
$base_path = \Directus\base_path();
}

$this->directus_path = $base_path;
$app = new Application($base_path, require $this->directus_path . '/config/api.php');
$configPath = InstallerUtils::createConfigPath($this->directus_path, $env);
$app = new Application($base_path, require $configPath);
$this->db = $app->getContainer()->get('database');

$this->settingsTableGateway = new TableGateway('directus_settings', $this->db);
Expand Down
25 changes: 18 additions & 7 deletions src/core/Directus/Console/Modules/InstallModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ public function cmdConfig($args, $extra)
$data['directus_email'] = $value;
break;
case 'c':
$data['cors_enabled'] = boolval($value);
$data['cors_enabled'] = (bool) $value;
break;
case 'E':
$data['env'] = (string) $value;
break;
}
}
Expand All @@ -102,21 +105,26 @@ public function cmdConfig($args, $extra)
throw new \Exception(sprintf('Path "%s" does not exists', $apiPath));
}

InstallerUtils::createConfig($data, $apiPath);
InstallerUtils::createConfig($directusPath, $data);
}

public function cmdDatabase($args, $extra)
{
$directus_path = $this->getBasePath() . DIRECTORY_SEPARATOR;
$env = null;

foreach ($args as $key => $value) {
switch ($key) {
case 'd':
$directus_path = $value;
break;
case 'E':
$env = $value;
break;
}
}

InstallerUtils::createTables($directus_path);
InstallerUtils::createTables($directus_path, $env);
}

public function cmdMigrate($args, $extra)
Expand All @@ -136,7 +144,7 @@ public function cmdSeeder($args, $extra)
public function cmdInstall($args, $extra)
{
$data = [];

$env = null;
$directus_path = $this->getBasePath() . DIRECTORY_SEPARATOR;

foreach ($args as $key => $value) {
Expand All @@ -156,15 +164,18 @@ public function cmdInstall($args, $extra)
case 'd':
$directus_path = $value;
break;
case 'E':
$env = $value;
break;
}
}

try {
$setting = new Setting($directus_path);
$setting = new Setting($directus_path, $env);

if (!$setting->isConfigured()) {
InstallerUtils::addDefaultSettings($data, $directus_path);
InstallerUtils::addDefaultUser($data, $directus_path);
InstallerUtils::addDefaultSettings($directus_path, $data, $env);
InstallerUtils::addDefaultUser($directus_path, $data, $env);
} else {
$setting->setSetting('global', 'project_name', $data['directus_name']);
// NOTE: Do we really want to change the email when re-run install command?
Expand Down
101 changes: 34 additions & 67 deletions src/core/Directus/Database/Schema/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
use Directus\Database\Schema\Object\Field;
use Directus\Database\Schema\Object\Collection;
use Directus\Database\Schema\Sources\SchemaInterface;
use Directus\Exception\Exception;
use Directus\Util\ArrayUtils;

class SchemaManager
{
// Tables
const COLLECTION_ACTIVITY = 'directus_activity';
const COLLECTION_ACTIVITY_SEEN = 'directus_activity_seen';
const COLLECTION_COLLECTIONS = 'directus_collections';
const COLLECTION_COLLECTION_PRESETS = 'directus_collection_presets';
const COLLECTION_FIELDS = 'directus_fields';
const COLLECTION_FILES = 'directus_files';
const COLLECTION_FOLDERS = 'directus_folders';
const COLLECTION_MIGRATIONS = 'directus_migrations';
const COLLECTION_ROLES = 'directus_roles';
const COLLECTION_PERMISSIONS = 'directus_permissions';
const COLLECTION_RELATIONS = 'directus_relations';
Expand Down Expand Up @@ -170,35 +172,6 @@ public function getField($tableName, $columnName, $skipCache = false)
return $columnSchema;
}

/**
* Add the system table prefix to to a table name.
*
* @param string|array $names
*
* @return array
*/
public function addSystemCollectionPrefix($names)
{
if (!is_array($names)) {
$names = [$names];
}

return array_map(function ($name) {
// TODO: Directus tables prefix _probably_ will be dynamic
return $this->prefix . $name;
}, $names);
}

/**
* Get Directus System tables name
*
* @return array
*/
public function getSystemCollections()
{
return $this->addSystemCollectionPrefix($this->directusTables);
}

/**
* Check if the given name is a system table
*
Expand All @@ -212,14 +185,15 @@ public function isSystemCollection($name)
}

/**
* Check if a table name exists
* Check if a collection exists
*
* @param string $collectionName
*
* @param $tableName
* @return bool
*/
public function tableExists($tableName)
public function collectionExists($collectionName)
{
return $this->source->collectionExists($tableName);
return $this->source->collectionExists($collectionName);
}

/**
Expand Down Expand Up @@ -468,39 +442,6 @@ public function castDefaultValue($value, $type, $length = null)
return $value;
}

/**
* Get all Directus system tables name
*
* @param array $filterNames
*
* @return array
*/
public function getDirectusCollections(array $filterNames = [])
{
$tables = $this->directusTables;
if ($filterNames) {
foreach ($tables as $i => $table) {
if (!in_array($table, $filterNames)) {
unset($tables[$i]);
}
}
}

return $this->addSystemCollectionPrefix($tables);
}

/**
* Check if a given table is a directus system table name
*
* @param $tableName
*
* @return bool
*/
public function isDirectusCollection($tableName)
{
return in_array($tableName, $this->getDirectusCollections());
}

/**
* Get the schema adapter
*
Expand Down Expand Up @@ -551,6 +492,32 @@ public static function getTemplates()
return $templatesData;
}

/**
* Returns all directus system collections name
*
* @return array
*/
public static function getSystemCollections()
{
return [
static::COLLECTION_ACTIVITY,
static::COLLECTION_ACTIVITY_SEEN,
static::COLLECTION_COLLECTIONS,
static::COLLECTION_COLLECTION_PRESETS,
static::COLLECTION_FIELDS,
static::COLLECTION_FILES,
static::COLLECTION_FOLDERS,
static::COLLECTION_MIGRATIONS,
static::COLLECTION_ROLES,
static::COLLECTION_PERMISSIONS,
static::COLLECTION_RELATIONS,
static::COLLECTION_REVISIONS,
static::COLLECTION_SETTINGS,
static::COLLECTION_USER_ROLES,
static::COLLECTION_USERS
];
}

/**
* Gets a collection object from an array attributes data
* @param $data
Expand Down
4 changes: 2 additions & 2 deletions src/core/Directus/Database/TableGateway/BaseTableGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ public function drop($tableName = null)
}

$dropped = false;
if ($this->schemaManager->tableExists($tableName)) {
if ($this->schemaManager->collectionExists($tableName)) {
// get drop table query
$sql = new Sql($this->adapter);
$drop = new Ddl\DropTable($tableName);
Expand Down Expand Up @@ -929,7 +929,7 @@ protected function getRawTableNameFromQueryStateTable($table)
public function convertDates(array $records, Collection $tableSchema, $tableName = null)
{
$tableName = $tableName === null ? $this->table : $tableName;
$isCustomTable = !$this->schemaManager->isDirectusCollection($tableName);
$isCustomTable = !$this->schemaManager->isSystemCollection($tableName);
$hasSystemDateColumn = $this->schemaManager->hasSystemDateField($tableName);

if (!$hasSystemDateColumn && $isCustomTable) {
Expand Down
11 changes: 11 additions & 0 deletions src/core/Directus/Exception/InvalidPathException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Directus\Exception;

class InvalidPathException extends Exception implements UnprocessableEntityExceptionInterface
{
public function __construct($message)
{
parent::__construct($message);
}
}
62 changes: 62 additions & 0 deletions src/core/Directus/Services/InstallService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Directus\Services;

use Directus\Util\ArrayUtils;
use Directus\Util\Installation\InstallerUtils;

class InstallService extends AbstractService
{
public function install(array $data)
{
$this->validate($data, [
'env' => 'string',

'force' => 'bool',

'db_host' => 'string',
'db_name' => 'required|string',
'db_user' => 'required|string',
'db_password' => 'string',

'mail_from' => 'string',
'cors_enabled' => 'bool',

'project_name' => 'string',
'user_email' => 'required|email',
'user_password' => 'required|string',
'user_token' => 'string'
]);

$force = ArrayUtils::pull($data, 'force', false);
$env = ArrayUtils::get($data, 'env', '_');
$basePath = $this->container->get('path_base');

InstallerUtils::ensureCanCreateConfig($basePath, $data, $force);
InstallerUtils::ensureCanCreateTables($basePath, $data, $force);

InstallerUtils::createConfig($basePath, $data, $force);

InstallerUtils::createTables($basePath, $env, $force);
InstallerUtils::addDefaultSettings($basePath, $data, $env);
InstallerUtils::addDefaultUser($basePath, $data, $env);

// config
// db_host
// db_name*
// db_user*
// db_password
// -----------
// mail_from
// cors_enabled

// database
// none

// default user/settings
// project_name => directus_name
// user_email => directus_email
// user_password => directus_password
// user_token => directus_token
}
}
4 changes: 2 additions & 2 deletions src/core/Directus/Services/TablesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public function updateTable($name, array $data, array $params = [])
$tableGateway->getOneData($name);
// ----------------------------------------------------------------------------

if (!$this->getSchemaManager()->tableExists($name)) {
if (!$this->getSchemaManager()->collectionExists($name)) {
throw new CollectionNotFoundException($name);
}

Expand Down Expand Up @@ -815,7 +815,7 @@ protected function dropColumnSchema($collectionName, $fieldName)
*/
public function dropTable($name)
{
if (!$this->getSchemaManager()->tableExists($name)) {
if (!$this->getSchemaManager()->collectionExists($name)) {
throw new CollectionNotFoundException($name);
}

Expand Down
Loading

0 comments on commit a1a17f9

Please sign in to comment.