/** * Create and return an instance of the Middleware. * * @param Interop\Container\ContainerInterface $container * @param string $requestedName * @param array $options * @return MiddlewareInterface */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { $rqlParser = new Rest\Rql\RqlParser(); $responseEncoder = new Rest\ResponseEncoder(); $resourceName = $requestedName; //is there config for this resource? $config = $container->get('config'); if (isset($config['resource'][$resourceName]['storeMiddleware'])) { $storeMiddlewareServiceName = $config['resource'][$resourceName]['storeMiddleware']; $storeMiddleware = $container->get($storeMiddlewareServiceName); return new restActionPipe($rqlParser, $storeMiddleware, $responseEncoder); } //is there table with same name? $db = $container->has('db') ? $container->get('db') : null; if (isset($db)) { $dbMetadata = new Zend\Db\Metadata\Metadata($db); $tableNames = $dbMetadata->getTableNames(); if (isset($tableNames[$resourceName])) { $tableGateway = new TableGateway($resourceName, $db); $dataStore = new DbTable($tableGateway); $storeMiddleware = new StoreMiddleware($dataStore); return new Rest\RestActionPipe($rqlParser, $storeMiddleware, $responseEncoder); } } throw new DataStoresException('Can\'t make RestActionPipe' . ' for resource: ' . $resourceName); }
/** * Generates PHP files with field names as constants */ public function generateDbConstants() { $metadata = new \Zend\Db\Metadata\Metadata($this->dbAdapter); $schema = $this->dbAdapter->getDriver()->getConnection()->getCurrentSchema(); $tables = $metadata->getTableNames($schema, true); foreach ($tables as $table) { $words = explode('_', $table); $class = 'Db'; foreach ($words as $word) { $word[0] = strtoupper($word[0]); $class = $class . $word; } $filename = __DIR__ . '/../Utils/DbConsts/' . $class . '.php'; if (file_exists($filename)) { unlink($filename); } $writer = new \Zend\Log\Writer\Stream($filename); $writer->setFormatter(new \Zend\Log\Formatter\Simple('%message%')); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info('<?php'); $logger->info(''); $logger->info('namespace Application\\Utils\\DbConsts;'); $logger->info(''); $logger->info("class {$class}"); $logger->info('{'); $logger->info(" const TABLE = '{$table}';"); $columns = $metadata->getColumnNames($table, $schema); foreach ($columns as $column) { $logger->info(vsprintf(" const %s = '%s';", array(strtoupper($column), $column))); } $logger->info(''); $hasConst = ' static public function hasField($field) { if (!is_string($field)) { return false; } $field = strtoupper($field); $reflect = new \\ReflectionClass(__CLASS__); foreach ($reflect->getConstants() as $name => $value) { if (strtoupper($value) === $field) { return true; } }; return false; }'; $logger->info($hasConst); $logger->info('}'); $logger = null; chmod($filename, 0777); } }
public function fetchAllByUser($user_id, $assoc = false) { $select = new Select($this->table); $select->columns(array('id', 'user', 'table_name', 'columns_visible', 'sort', 'sort_order', STATUS_COLUMN_NAME, 'title', 'search_string')); $select->where->equalTo('user', $user_id)->isNull('title'); $select->where('table_name NOT IN( "directus_columns", "directus_ip_whitelist", "directus_preferences", "directus_privileges", "directus_settings", "directus_storage_adapters", "directus_tables", "directus_tab_privileges", "directus_ui" )'); $metadata = new \Zend\Db\Metadata\Metadata($this->getAdapter()); $tables = $metadata->getTableNames(DB_NAME); $tables = array_diff($tables, array("directus_columns", "directus_ip_whitelist", "directus_preferences", "directus_privileges", "directus_settings", "directus_storage_adapters", "directus_tables", "directus_tab_privileges", "directus_ui")); $rows = $this->selectWith($select)->toArray(); $preferences = array(); $tablePrefs = array(); foreach ($rows as $row) { $tablePrefs[$row['table_name']] = $row; } //Get Default Preferences foreach ($tables as $key => $table) { // Honor ACL. Skip the tables that the user doesn't have access too if (!TableSchema::canGroupViewTable($table)) { continue; } $tableName = $table; if (!isset($tablePrefs[$table])) { $table = null; } else { $table = $tablePrefs[$table]; } if (!isset($table['user'])) { $table = null; } $table = $this->constructPreferences($user_id, $tableName, $table); $preferences[$tableName] = $table; } return $preferences; }
/** */ public function generateDbMapper() { $metadata = new \Zend\Db\Metadata\Metadata($this->adapter); // get the table names $tableNames = $metadata->getTableNames(); echo "<pre>"; foreach ($tableNames as $table) { echo '<br>Creating<strong> ' . $table . "</strong> "; $this->generateDirectoryPath(); $this->generateClassFile($table, $metadata->getTable($table)->getColumns()); } echo '<hr>---'; }
/** * Get a list of Galette's tables * * @param string $prefix Specified table prefix, PREFIX_DB if null * * @return array */ public function getTables($prefix = null) { $metadata = new \Zend\Db\Metadata\Metadata($this->_db); $tmp_tables_list = $metadata->getTableNames(); if ($prefix === null) { $prefix = PREFIX_DB; } $tables_list = array(); //filter table_list: we only want PREFIX_DB tables foreach ($tmp_tables_list as $t) { if (preg_match('/^' . $prefix . '/', $t)) { $tables_list[] = $t; } } return $tables_list; }
public function fetchAllByUser($user_id, $assoc = false) { $select = new Select($this->table); $select->columns(['id', 'user', 'table_name', 'columns_visible', 'sort', 'sort_order', 'status', 'title', 'search_string']); $select->where->equalTo('user', $user_id)->isNull('title'); $coreTables = SchemaManager::getDirectusTables(static::$IGNORED_TABLES); $select->where->addPredicate(new NotIn('table_name', $coreTables)); $metadata = new \Zend\Db\Metadata\Metadata($this->getAdapter()); $tables = $metadata->getTableNames(); $tables = array_diff($tables, $coreTables); $rows = $this->selectWith($select)->toArray(); $preferences = []; $tablePrefs = []; foreach ($rows as $row) { $tablePrefs[$row['table_name']] = $row; } //Get Default Preferences foreach ($tables as $key => $table) { // Honor ACL. Skip the tables that the user doesn't have access too if (!TableSchema::canGroupViewTable($table)) { continue; } $tableName = $table; if (!isset($tablePrefs[$table])) { $table = null; } else { $table = $tablePrefs[$table]; } if (!isset($table['user'])) { $table = null; } $table = $this->constructPreferences($user_id, $tableName, $table); $preferences[$tableName] = $table; } return $preferences; }
<?php $adapter = (include file_exists('bootstrap.php') ? 'bootstrap.php' : 'bootstrap.dist.php'); refresh_data($adapter); $metadata = new Zend\Db\Metadata\Metadata($adapter); // get the table names $tableNames = $metadata->getTableNames(); foreach ($tableNames as $tableName) { echo 'In Table ' . $tableName . PHP_EOL; $table = $metadata->getTable($tableName); echo ' With columns: ' . PHP_EOL; foreach ($table->getColumns() as $column) { echo ' ' . $column->getName() . ' -> ' . $column->getDataType() . PHP_EOL; } echo PHP_EOL; echo ' With constraints: ' . PHP_EOL; foreach ($metadata->getConstraints($tableName) as $constraint) { /** @var $constraint Zend\Db\Metadata\Object\ConstraintObject */ echo ' ' . $constraint->getName() . ' -> ' . $constraint->getType() . PHP_EOL; if (!$constraint->hasColumns()) { continue; } echo ' column: ' . implode(', ', $constraint->getColumns()); if ($constraint->isForeignKey()) { $fkCols = array(); foreach ($constraint->getReferencedColumns() as $refColumn) { $fkCols[] = $constraint->getReferencedTableName() . '.' . $refColumn; } echo ' => ' . implode(', ', $fkCols); } echo PHP_EOL;