diff --git a/.github/workflows/github-actions.yml b/.github/workflows/github-actions.yml deleted file mode 100644 index e69de29..0000000 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..a091580 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,28 @@ +name: test + +on: [push] + +concurrency: production_environment + +jobs: + test: + runs-on: ubuntu-latest + + name: Check code style + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.1 + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo + coverage: none + + - name: Install PHP dependencies (composer) + run: composer install + + - name: Check code style + run: composer phpcs diff --git a/README.md b/README.md index 9129868..880b50a 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,22 @@ # catalogs-php A PHP framework to talk to Catalogs. Part of the [CloudFest 2024 Hackathon](https://hackathon.cloudfest.com/project/integrating-mariadb-catalogs-with-php-platforms/). -# Useage +# Installation Include the catalog-php with composer: - composer require mariadb/catalogs-php +```bash +composer require mariadb/catalogs-php:dev-main +``` +Include autoloader in your project. +```php + - diff --git a/src/Catalog.php b/src/Catalog.php index 6bc59e3..efcb25e 100644 --- a/src/Catalog.php +++ b/src/Catalog.php @@ -22,6 +22,7 @@ class Catalog // This is too low, because this is a beta version we are developing for. public const MINIMAL_MARIA_VERSION = '11.0.2'; + /** * Class constructor. * @@ -81,7 +82,7 @@ public function __construct( /** * Create a new catalog * - * @param string $catName The new Catalog name. + * @param string $catName The new Catalog name. * * @return int */ @@ -92,7 +93,7 @@ public function create(string $catName): int throw new Exception('Catalog name already exists.'); } - $root_privileges = $this->connection->query("SELECT * FROM mysql.global_priv WHERE User='{$this->dbUser}' AND Host='%';"); + $rootPrivileges = $this->connection->query("SELECT * FROM mysql.global_priv WHERE User='{$this->dbUser}' AND Host='%';"); $scripts = [ 'src/create_catalog_sql/mysql_system_tables.sql', @@ -101,8 +102,8 @@ public function create(string $catName): int 'src/create_catalog_sql/maria_add_gis_sp.sql', 'src/create_catalog_sql/mysql_sys_schema.sql', ]; - $this->connection->exec('CREATE CATALOG IF NOT EXISTS ' .$catName); - $this->connection->exec('USE CATALOG ' . $catName); + $this->connection->exec('CREATE CATALOG IF NOT EXISTS '.$catName); + $this->connection->exec('USE CATALOG '.$catName); $this->connection->exec('CREATE DATABASE IF NOT EXISTS mysql'); $this->connection->exec('USE mysql'); @@ -125,13 +126,17 @@ public function create(string $catName): int $this->connection->exec($content); } - if ($root_privileges->rowCount() > 0) { - foreach ($root_privileges as $privilege) { - $this->connection->exec("INSERT INTO mysql.global_priv VALUES ('{$privilege['Host']}', '{$privilege['User']}', '{$privilege['Priv']}');"); + if ($rootPrivileges->rowCount() > 0) { + foreach ($rootPrivileges as $privilege) { + $host = $privilege['Host']; + $user = $privilege['User']; + $priv = $privilege['Priv']; + $this->connection->exec("INSERT INTO mysql.global_priv VALUES ('{$host}', '{$user}', '{$priv}');"); } } return $this->getPort($catName); + } @@ -186,26 +191,26 @@ public function drop(string $catName): bool ); try { - // enter the catalog - $this->connection->exec('USE CATALOG ' . $catName); + // Enter the catalog. + $this->connection->exec('USE CATALOG '.$catName); - // check if there are any tables besides mysql, sys, performance_schema and information_schema + // Check if there are any tables besides mysql, sys, performance_schema and information_schema. $tables = $this->connection->query('SHOW DATABASES'); foreach ($tables as $table) { - if (!in_array($table['Database'], ['mysql', 'sys', 'performance_schema', 'information_schema'])) { + if (in_array($table['Database'], ['mysql', 'sys', 'performance_schema', 'information_schema']) === false) { throw new \Exception('Catalog is not empty'); } } - // drop mysql, sys and performance_schema + // Drop mysql, sys and performance_schema. $this->connection->exec('DROP DATABASE IF EXISTS mysql'); $this->connection->exec('DROP DATABASE IF EXISTS sys'); $this->connection->exec('DROP DATABASE IF EXISTS performance_schema'); - // drop the catalog - $this->connection->exec('DROP CATALOG ' . $catName); + // Drop the catalog. + $this->connection->exec('DROP CATALOG '.$catName); } catch (\PDOException $e) { - throw new \Exception('Error dropping catalog: ' . $e->getMessage()); + throw new \Exception('Error dropping catalog: '.$e->getMessage()); } return true; @@ -226,17 +231,41 @@ private function alter() } + /** + * Create admin user for a catalog + * + * @param string $catalog The catalog name + * @param string $userName The user name + * @param string $password The user password + * @param string $authHost The database host + * * @return void */ - public function createAdminUserForCatalog(string $catalog, string $userName, string $password, string $authHost = 'localhost'): void - { + public function createAdminUserForCatalog( + string $catalog, + string $userName, + string $password, + string $authHost='localhost' + ): void { $this->connection->exec("USE CATALOG {$catalog}"); $this->connection->exec("USE mysql"); - $this->connection = new \PDO("mysql:host={$this->dbHost};port={$this->dbPort};dbname={$catalog}.mysql", $this->dbUser, $this->dbPass, $this->dbOptions); + $this->connection = new \PDO( + "mysql:host={$this->dbHost};port={$this->dbPort};dbname={$catalog}.mysql", + $this->dbUser, + $this->dbPass, + $this->dbOptions + ); + + $this->connection->prepare( + "CREATE USER ?@? IDENTIFIED BY ?;" + )->execute([$userName, $authHost, $password]); + $this->connection->prepare( + "GRANT ALL PRIVILEGES ON `%`.* TO ?@? IDENTIFIED BY ? WITH GRANT OPTION;" + )->execute([$userName, $authHost, $password]); - $this->connection->prepare("CREATE USER ?@? IDENTIFIED BY ?;")->execute([$userName, $authHost, $password]); - $this->connection->prepare("GRANT ALL PRIVILEGES ON `%`.* TO ?@? IDENTIFIED BY ? WITH GRANT OPTION;")->execute([$userName, $authHost,$password]); } + + }