From e1827e22851dddd897ec7224ddb17e61a85c5440 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 11 Feb 2024 00:08:34 -0500 Subject: [PATCH 1/9] Trim out datadomain, namespace and token replacement methods Also remove factory methods that we won't be using as configuration is generated from cli options like source/plugin/connection now. --- src/Config/Config.php | 242 +---------------------- src/Migration/Manager.php | 18 +- tests/TestCase/Config/ConfigTest.php | 49 ----- tests/TestCase/Migration/ManagerTest.php | 8 - 4 files changed, 7 insertions(+), 310 deletions(-) diff --git a/src/Config/Config.php b/src/Config/Config.php index ace778f0..c627b961 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -10,19 +10,16 @@ use Closure; use InvalidArgumentException; -use Phinx\Config\ConfigInterface as PhinxConfigInterface; use Phinx\Db\Adapter\SQLiteAdapter; -use Phinx\Util\Util; use Psr\Container\ContainerInterface; use ReturnTypeWillChange; use RuntimeException; -use Symfony\Component\Yaml\Yaml; use UnexpectedValueException; /** - * Phinx configuration class. + * Migrations configuration class. */ -class Config implements ConfigInterface, PhinxConfigInterface +class Config implements ConfigInterface { /** * The value that identifies a version order by creation time. @@ -54,90 +51,7 @@ class Config implements ConfigInterface, PhinxConfigInterface public function __construct(array $configArray, ?string $configFilePath = null) { $this->configFilePath = $configFilePath; - $this->values = $this->replaceTokens($configArray); - } - - /** - * Create a new instance of the config class using a Yaml file path. - * - * @param string $configFilePath Path to the Yaml File - * @throws \RuntimeException - * @return \Migrations\Config\ConfigInterface - * @deprecated 4.2 To be removed in 5.x - */ - public static function fromYaml(string $configFilePath): ConfigInterface - { - if (!class_exists('Symfony\\Component\\Yaml\\Yaml', true)) { - // @codeCoverageIgnoreStart - throw new RuntimeException('Missing yaml parser, symfony/yaml package is not installed.'); - // @codeCoverageIgnoreEnd - } - - $configFile = file_get_contents($configFilePath); - $configArray = Yaml::parse($configFile); - - if (!is_array($configArray)) { - throw new RuntimeException(sprintf( - 'File \'%s\' must be valid YAML', - $configFilePath - )); - } - - return new Config($configArray, $configFilePath); - } - - /** - * Create a new instance of the config class using a JSON file path. - * - * @param string $configFilePath Path to the JSON File - * @throws \RuntimeException - * @return \Migrations\Config\ConfigInterface - * @deprecated 4.2 To be removed in 5.x - */ - public static function fromJson(string $configFilePath): ConfigInterface - { - if (!function_exists('json_decode')) { - // @codeCoverageIgnoreStart - throw new RuntimeException('Need to install JSON PHP extension to use JSON config'); - // @codeCoverageIgnoreEnd - } - - $configArray = json_decode((string)file_get_contents($configFilePath), true); - if (!is_array($configArray)) { - throw new RuntimeException(sprintf( - 'File \'%s\' must be valid JSON', - $configFilePath - )); - } - - return new Config($configArray, $configFilePath); - } - - /** - * Create a new instance of the config class using a PHP file path. - * - * @param string $configFilePath Path to the PHP File - * @throws \RuntimeException - * @return \Migrations\Config\ConfigInterface - * @deprecated 4.2 To be removed in 5.x - */ - public static function fromPhp(string $configFilePath): ConfigInterface - { - ob_start(); - /** @noinspection PhpIncludeInspection */ - $configArray = include $configFilePath; - - // Hide console output - ob_end_clean(); - - if (!is_array($configArray)) { - throw new RuntimeException(sprintf( - 'PHP file \'%s\' must return an array', - $configFilePath - )); - } - - return new Config($configArray, $configFilePath); + $this->values = $configArray; } /** @@ -184,7 +98,7 @@ public function getEnvironment(string $name): ?array $environments[$name]['name'] = SQLiteAdapter::MEMORY; } - return $this->parseAgnosticDsn($environments[$name]); + return $environments[$name]; } return null; @@ -358,19 +272,6 @@ public function getTemplateStyle(): string return $this->values['templates']['style'] === self::TEMPLATE_STYLE_UP_DOWN ? self::TEMPLATE_STYLE_UP_DOWN : self::TEMPLATE_STYLE_CHANGE; } - /** - * @inheritdoc - * @deprecated 4.2 To be removed in 5.x - */ - public function getDataDomain(): array - { - if (!isset($this->values['data_domain'])) { - return []; - } - - return $this->values['data_domain']; - } - /** * @inheritDoc */ @@ -405,95 +306,6 @@ public function isVersionOrderCreationTime(): bool return $versionOrder == self::VERSION_ORDER_CREATION_TIME; } - /** - * @inheritdoc - * @deprecated 4.2 To be removed in 5.x - */ - public function getBootstrapFile(): string|false - { - if (!isset($this->values['paths']['bootstrap'])) { - return false; - } - - return $this->values['paths']['bootstrap']; - } - - /** - * Replace tokens in the specified array. - * - * @param array $arr Array to replace - * @return array - */ - protected function replaceTokens(array $arr): array - { - // Get environment variables - // Depending on configuration of server / OS and variables_order directive, - // environment variables either end up in $_SERVER (most likely) or $_ENV, - // so we search through both - - /** @var array $tokens */ - $tokens = []; - foreach (array_merge($_ENV, $_SERVER) as $varname => $varvalue) { - if (strpos($varname, 'PHINX_') === 0) { - $tokens['%%' . $varname . '%%'] = $varvalue; - } - } - - // Phinx defined tokens (override env tokens) - $tokens['%%PHINX_CONFIG_PATH%%'] = $this->getConfigFilePath(); - $tokens['%%PHINX_CONFIG_DIR%%'] = $this->getConfigFilePath() !== null ? dirname((string)$this->getConfigFilePath()) : ''; - - // Recurse the array and replace tokens - return $this->recurseArrayForTokens($arr, $tokens); - } - - /** - * Recurse an array for the specified tokens and replace them. - * - * @param array $arr Array to recurse - * @param array $tokens Array of tokens to search for - * @return array - */ - protected function recurseArrayForTokens(array $arr, array $tokens): array - { - /** @var array $out */ - $out = []; - foreach ($arr as $name => $value) { - if (is_array($value)) { - $out[$name] = $this->recurseArrayForTokens($value, $tokens); - continue; - } - if (is_string($value)) { - foreach ($tokens as $token => $tval) { - $value = str_replace($token, $tval ?? '', $value); - } - $out[$name] = $value; - continue; - } - $out[$name] = $value; - } - - return $out; - } - - /** - * Parse a database-agnostic DSN into individual options. - * - * @param array $options Options - * @return array - */ - protected function parseAgnosticDsn(array $options): array - { - $parsed = Util::parseDsn($options['dsn'] ?? ''); - if ($parsed) { - unset($options['dsn']); - } - - $options += $parsed; - - return $options; - } - /** * {@inheritDoc} * @@ -552,50 +364,4 @@ public function getSeedTemplateFile(): ?string { return $this->values['templates']['seedFile'] ?? null; } - - /** - * Search $needle in $haystack and return key associate with him. - * - * @param string $needle Needle - * @param string[] $haystack Haystack - * @return string|null - * @deprecated 4.2 To be removed in 5.x - */ - protected function searchNamespace(string $needle, array $haystack): ?string - { - $needle = realpath($needle); - $haystack = array_map('realpath', $haystack); - - $key = array_search($needle, $haystack, true); - - return is_string($key) ? trim($key, '\\') : null; - } - - /** - * Get Migration Namespace associated with path. - * - * @param string $path Path - * @return string|null - * @deprecated 4.2 To be removed in 5.x - */ - public function getMigrationNamespaceByPath(string $path): ?string - { - $paths = $this->getMigrationPaths(); - - return $this->searchNamespace($path, $paths); - } - - /** - * Get Seed Namespace associated with path. - * - * @param string $path Path - * @return string|null - * @deprecated 4.2 To be removed in 5.x - */ - public function getSeedNamespaceByPath(string $path): ?string - { - $paths = $this->getSeedPaths(); - - return $this->searchNamespace($path, $paths); - } } diff --git a/src/Migration/Manager.php b/src/Migration/Manager.php index 6673e7e3..62db802b 100644 --- a/src/Migration/Manager.php +++ b/src/Migration/Manager.php @@ -752,9 +752,6 @@ public function getEnvironment(string $name): Environment // create an environment instance and cache it $envOptions = $config->getEnvironment($name); $envOptions['version_order'] = $config->getVersionOrder(); - if ($config instanceof Config) { - $envOptions['data_domain'] = $config->getDataDomain(); - } $environment = new Environment($name, $envOptions); $this->environments[$name] = $environment; @@ -878,14 +875,8 @@ function ($phpFile) { throw new InvalidArgumentException(sprintf('Duplicate migration - "%s" has the same version as "%s"', $filePath, $versions[$version]->getVersion())); } - $config = $this->getConfig(); - $namespace = null; - if ($config instanceof Config) { - $namespace = $config->getMigrationNamespaceByPath(dirname($filePath)); - } - // convert the filename to a class name - $class = ($namespace === null ? '' : $namespace . '\\') . Util::mapFileNameToClassName(basename($filePath)); + $class = Util::mapFileNameToClassName(basename($filePath)); if (isset($fileNames[$class])) { throw new InvalidArgumentException(sprintf( @@ -1031,13 +1022,9 @@ public function getSeeds(string $environment): array foreach ($phpFiles as $filePath) { if (Util::isValidSeedFileName(basename($filePath))) { $config = $this->getConfig(); - $namespace = null; - if ($config instanceof Config) { - $namespace = $config->getSeedNamespaceByPath(dirname($filePath)); - } // convert the filename to a class name - $class = ($namespace === null ? '' : $namespace . '\\') . pathinfo($filePath, PATHINFO_FILENAME); + $class = pathinfo($filePath, PATHINFO_FILENAME); $fileNames[$class] = basename($filePath); // load the seed file @@ -1058,6 +1045,7 @@ public function getSeeds(string $environment): array } else { $seed = new $class(); } + // TODO might need a migrations -> phinx shim here for Environment $seed->setEnvironment($environment); $input = $this->getInput(); $seed->setInput($input); diff --git a/tests/TestCase/Config/ConfigTest.php b/tests/TestCase/Config/ConfigTest.php index 5f2b61a8..0830cd89 100644 --- a/tests/TestCase/Config/ConfigTest.php +++ b/tests/TestCase/Config/ConfigTest.php @@ -73,25 +73,6 @@ public function testHasEnvironmentMethod() $this->assertFalse($config->hasEnvironment('fakeenvironment')); } - /** - * @covers \Phinx\Config\Config::getDataDomain - */ - public function testGetDataDomainMethod() - { - $config = new Config($this->getConfigArray()); - $this->assertIsArray($config->getDataDomain()); - } - - /** - * @covers \Phinx\Config\Config::getDataDomain - */ - public function testReturnsEmptyArrayWithEmptyDataDomain() - { - $config = new Config([]); - $this->assertIsArray($config->getDataDomain()); - $this->assertCount(0, $config->getDataDomain()); - } - /** * @covers \Phinx\Config\Config::getDefaultEnvironment */ @@ -305,36 +286,6 @@ public static function isVersionOrderCreationTimeDataProvider() ]; } - public function testConfigReplacesEnvironmentTokens() - { - $_SERVER['PHINX_TEST_CONFIG_ADAPTER'] = 'sqlite'; - $_SERVER['PHINX_TEST_CONFIG_SUFFIX'] = 'sqlite3'; - $_ENV['PHINX_TEST_CONFIG_NAME'] = 'phinx_testing'; - $_ENV['PHINX_TEST_CONFIG_SUFFIX'] = 'foo'; - - try { - $config = new Config([ - 'environments' => [ - 'production' => [ - 'adapter' => '%%PHINX_TEST_CONFIG_ADAPTER%%', - 'name' => '%%PHINX_TEST_CONFIG_NAME%%', - 'suffix' => '%%PHINX_TEST_CONFIG_SUFFIX%%', - ], - ], - ]); - - $this->assertSame( - ['adapter' => 'sqlite', 'name' => 'phinx_testing', 'suffix' => 'sqlite3'], - $config->getEnvironment('production') - ); - } finally { - unset($_SERVER['PHINX_TEST_CONFIG_ADAPTER']); - unset($_SERVER['PHINX_TEST_CONFIG_SUFFIX']); - unset($_ENV['PHINX_TEST_CONFIG_NAME']); - unset($_ENV['PHINX_TEST_CONFIG_SUFFIX']); - } - } - public function testSqliteMemorySetsName() { $config = new Config([ diff --git a/tests/TestCase/Migration/ManagerTest.php b/tests/TestCase/Migration/ManagerTest.php index 0c60ac43..7f1412e6 100644 --- a/tests/TestCase/Migration/ManagerTest.php +++ b/tests/TestCase/Migration/ManagerTest.php @@ -169,14 +169,6 @@ public function testInstantiation() ); } - public function testEnvironmentInheritsDataDomainOptions() - { - foreach ($this->config->getEnvironments() as $name => $opts) { - $env = $this->manager->getEnvironment($name); - $this->assertArrayHasKey('data_domain', $env->getOptions()); - } - } - public function testPrintStatusMethod() { // stub environment From 3c69daa2ef5b5e0324bd7c610556f5b19f131615 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 11 Feb 2024 00:17:25 -0500 Subject: [PATCH 2/9] Remove apc config this extension isn't used any more. --- phpunit.xml.dist | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f9aaecac..d86e5b48 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,7 +3,7 @@ colors="true" cacheDirectory=".phpunit.cache" bootstrap="tests/bootstrap.php" - xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" > @@ -34,7 +34,6 @@ -