diff --git a/CHANGELOG.md b/CHANGELOG.md index 7718231..bbb8024 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ # Version History -**1.0.0** (DATE DATE DATE) +** UNRELEASED ** + +* Remove support for 'wrapping' database adapters, and the `TimedOutputAdapter`. The timed adapter was the only + remaining implementation of this proxy pattern, and was only actually printing statistics for `insert()` operations. + This was not that useful, and the overhead (e.g. on identifying dead code / improving type safety / adding to + adapters in future) of the proxy pattern was quite high relative to the benefit. You can, of course, implement and + register your own adapter class as a proxy if you need it. + +**1.0.0 BETA** (2022-10-21) * First release of the phinx-lightweight forked library * Removed support for all database platforms/drivers other than MySQL over PDO diff --git a/src/Phinx/Db/Adapter/AdapterFactory.php b/src/Phinx/Db/Adapter/AdapterFactory.php index bedf3a0..c72ed1d 100644 --- a/src/Phinx/Db/Adapter/AdapterFactory.php +++ b/src/Phinx/Db/Adapter/AdapterFactory.php @@ -65,15 +65,6 @@ public static function instance() 'mysql' => 'Phinx\Db\Adapter\MysqlAdapter', ]; - /** - * Class map of adapters wrappers, indexed by name. - * - * @var array - */ - protected $wrappers = [ - 'timed' => 'Phinx\Db\Adapter\TimedOutputAdapter', - ]; - /** * Add or replace an adapter with a fully qualified class name. * @@ -128,45 +119,6 @@ public function getAdapter($name, array $options) return new $class($options); } - /** - * Add or replace a wrapper with a fully qualified class name. - * - * @throws \RuntimeException - * @param string $name - * @param string $class - * @return $this - */ - public function registerWrapper($name, $class) - { - if (!is_subclass_of($class, 'Phinx\Db\Adapter\WrapperInterface')) { - throw new \RuntimeException(sprintf( - 'Wrapper class "%s" must be implement Phinx\\Db\\Adapter\\WrapperInterface', - $class - )); - } - $this->wrappers[$name] = $class; - - return $this; - } - - /** - * Get a wrapper class by name. - * - * @throws \RuntimeException - * @param string $name - * @return string - */ - protected function getWrapperClass($name) - { - if (empty($this->wrappers[$name])) { - throw new \RuntimeException(sprintf( - 'Wrapper "%s" has not been registered', - $name - )); - } - - return $this->wrappers[$name]; - } /** * Get a wrapper instance by name. diff --git a/src/Phinx/Db/Adapter/AdapterWrapper.php b/src/Phinx/Db/Adapter/AdapterWrapper.php deleted file mode 100644 index 30a4d89..0000000 --- a/src/Phinx/Db/Adapter/AdapterWrapper.php +++ /dev/null @@ -1,304 +0,0 @@ - - */ -abstract class AdapterWrapper implements AdapterInterface, WrapperInterface -{ - /** - * @var \Phinx\Db\Adapter\AdapterInterface - */ - protected $adapter; - - /** - * {@inheritdoc} - */ - public function __construct(AdapterInterface $adapter) - { - $this->setAdapter($adapter); - } - - /** - * {@inheritdoc} - */ - public function setAdapter(AdapterInterface $adapter) - { - $this->adapter = $adapter; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getAdapter() - { - return $this->adapter; - } - - /** - * {@inheritdoc} - */ - public function setOptions(array $options) - { - $this->adapter->setOptions($options); - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getOptions() - { - return $this->adapter->getOptions(); - } - - /** - * {@inheritdoc} - */ - public function hasOption($name) - { - return $this->adapter->hasOption($name); - } - - /** - * {@inheritdoc} - */ - public function getOption($name) - { - return $this->adapter->getOption($name); - } - - /** - * {@inheritdoc} - */ - public function setInput(InputInterface $input) - { - $this->adapter->setInput($input); - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getInput() - { - return $this->adapter->getInput(); - } - - /** - * {@inheritdoc} - */ - public function setOutput(OutputInterface $output) - { - $this->adapter->setOutput($output); - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getOutput() - { - return $this->adapter->getOutput(); - } - - /** - * {@inheritdoc} - */ - public function connect() - { - $this->getAdapter()->connect(); - } - - /** - * {@inheritdoc} - */ - public function disconnect() - { - $this->getAdapter()->disconnect(); - } - - /** - * {@inheritdoc} - */ - public function execute(string $sql): false|int - { - return $this->getAdapter()->execute($sql); - } - - /** - * {@inheritdoc} - */ - public function query(string $sql): \PDOStatement - { - return $this->getAdapter()->query($sql); - } - - /** - * {@inheritdoc} - */ - public function insert(string $table_name, array $row): void - { - $this->getAdapter()->insert($table_name, $row); - } - - /** - * {@inheritdoc} - */ - public function bulkinsert(string $table_name, array $rows): void - { - $this->getAdapter()->bulkinsert($table_name, $rows); - } - - /** - * {@inheritdoc} - */ - public function fetchRow(string $sql): array|false - { - return $this->getAdapter()->fetchRow($sql); - } - - /** - * {@inheritdoc} - */ - public function fetchAll(string $sql): array - { - return $this->getAdapter()->fetchAll($sql); - } - - /** - * {@inheritdoc} - */ - public function getVersions() - { - return $this->getAdapter()->getVersions(); - } - - /** - * {@inheritdoc} - */ - public function getVersionLog() - { - return $this->getAdapter()->getVersionLog(); - } - - /** - * {@inheritdoc} - */ - public function migrated(MigrationInterface $migration, $startTime, $endTime) - { - $this->getAdapter()->migrated($migration, $startTime, $endTime); - - return $this; - } - - /** - * {@inheritdoc} - */ - public function hasTransactions() - { - return $this->getAdapter()->hasTransactions(); - } - - /** - * {@inheritdoc} - */ - public function beginTransaction() - { - $this->getAdapter()->beginTransaction(); - } - - /** - * {@inheritdoc} - */ - public function commitTransaction() - { - $this->getAdapter()->commitTransaction(); - } - - /** - * {@inheritdoc} - */ - public function rollbackTransaction() - { - $this->getAdapter()->rollbackTransaction(); - } - - /** - * {@inheritdoc} - */ - public function quoteTableName($tableName) - { - return $this->getAdapter()->quoteTableName($tableName); - } - - /** - * {@inheritdoc} - */ - public function quoteColumnName($columnName) - { - return $this->getAdapter()->quoteColumnName($columnName); - } - - /** - * {@inheritdoc} - */ - public function hasTable($tableName) - { - return $this->getAdapter()->hasTable($tableName); - } - - /** - * {@inheritdoc} - */ - public function getConnection(): \PDO - { - return $this->getAdapter()->getConnection(); - } -} diff --git a/src/Phinx/Db/Adapter/TimedOutputAdapter.php b/src/Phinx/Db/Adapter/TimedOutputAdapter.php deleted file mode 100644 index 195ad97..0000000 --- a/src/Phinx/Db/Adapter/TimedOutputAdapter.php +++ /dev/null @@ -1,131 +0,0 @@ -getAdapter()->getAdapterType(); - } - - /** - * Start timing a command. - * - * @return callable A function that is to be called when the command finishes - */ - public function startCommandTimer() - { - $started = microtime(true); - - return function () use ($started) { - $end = microtime(true); - if (OutputInterface::VERBOSITY_VERBOSE <= $this->getOutput()->getVerbosity()) { - $this->getOutput()->writeln(' -> ' . sprintf('%.4fs', $end - $started)); - } - }; - } - - /** - * Write a Phinx command to the output. - * - * @param string $command Command Name - * @param array $args Command Args - * @return void - */ - public function writeCommand($command, $args = []) - { - if (OutputInterface::VERBOSITY_VERBOSE > $this->getOutput()->getVerbosity()) { - return; - } - - if (count($args)) { - $outArr = []; - foreach ($args as $arg) { - if (is_array($arg)) { - $arg = array_map( - function ($value) { - return '\'' . $value . '\''; - }, - $arg - ); - $outArr[] = '[' . implode(', ', $arg) . ']'; - continue; - } - - $outArr[] = '\'' . $arg . '\''; - } - $this->getOutput()->writeln(' -- ' . $command . '(' . implode(', ', $outArr) . ')'); - - return; - } - - $this->getOutput()->writeln(' -- ' . $command); - } - - /** - * {@inheritdoc} - * - * @return void - */ - public function insert(string $table_name, array $row): void - { - $end = $this->startCommandTimer(); - $this->writeCommand('insert', [$table_name]); - parent::insert($table_name, $row); - $end(); - } - - /** - * {@inheritdoc} - * - * @return void - */ - public function bulkinsert(string $table_name, array $rows): void - { - $end = $this->startCommandTimer(); - $this->writeCommand('bulkinsert', [$table_name]); - parent::bulkinsert($table_name, $rows); - $end(); - } - -} diff --git a/src/Phinx/Db/Adapter/WrapperInterface.php b/src/Phinx/Db/Adapter/WrapperInterface.php deleted file mode 100644 index af8b3d0..0000000 --- a/src/Phinx/Db/Adapter/WrapperInterface.php +++ /dev/null @@ -1,60 +0,0 @@ - - */ -interface WrapperInterface -{ - /** - * Class constructor, must always wrap another adapter. - * - * @param \Phinx\Db\Adapter\AdapterInterface $adapter - */ - public function __construct(AdapterInterface $adapter); - - /** - * Sets the database adapter to proxy commands to. - * - * @param \Phinx\Db\Adapter\AdapterInterface $adapter - * @return \Phinx\Db\Adapter\AdapterInterface - */ - public function setAdapter(AdapterInterface $adapter); - - /** - * Gets the database adapter. - * - * @throws \RuntimeException if the adapter has not been set - * @return \Phinx\Db\Adapter\AdapterInterface - */ - public function getAdapter(); -} diff --git a/src/Phinx/Migration/Manager/Environment.php b/src/Phinx/Migration/Manager/Environment.php index 153e1e5..b223c9b 100644 --- a/src/Phinx/Migration/Manager/Environment.php +++ b/src/Phinx/Migration/Manager/Environment.php @@ -299,12 +299,8 @@ public function getAdapter() $adapter = $factory ->getAdapter($this->options['adapter'], $this->options); - // Automatically time the executed commands - $adapter = $factory->getWrapper('timed', $adapter); - if (isset($this->options['wrapper'])) { - $adapter = $factory - ->getWrapper($this->options['wrapper'], $adapter); + throw new \InvalidArgumentException('The `wrapper` option has been removed'); } if ($this->getInput()) { diff --git a/tests/Phinx/Db/Adapter/AdapterFactoryTest.php b/tests/Phinx/Db/Adapter/AdapterFactoryTest.php index d2dad95..0033223 100644 --- a/tests/Phinx/Db/Adapter/AdapterFactoryTest.php +++ b/tests/Phinx/Db/Adapter/AdapterFactoryTest.php @@ -3,7 +3,6 @@ namespace Test\Phinx\Db\Adapter; use Phinx\Db\Adapter\AdapterFactory; -use Phinx\Db\Adapter\TimedOutputAdapter; use PHPUnit\Framework\TestCase; class AdapterFactoryTest extends TestCase @@ -63,43 +62,4 @@ public function testGetAdapterFailure() $this->factory->getAdapter('bad', []); } - public function testRegisterWrapper() - { - // WrapperFactory::getClass is protected, work around it to avoid - // creating unnecessary instances and making the test more complex. - $method = new \ReflectionMethod(get_class($this->factory), 'getWrapperClass'); - $method->setAccessible(true); - - $wrapper = $method->invoke($this->factory, 'timed'); - $this->factory->registerWrapper('test', $wrapper); - - $this->assertEquals($wrapper, $method->invoke($this->factory, 'test')); - } - - public function testRegisterWrapperFailure() - { - $wrapper = get_class($this); - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('Wrapper class "Test\Phinx\Db\Adapter\AdapterFactoryTest" must be implement Phinx\Db\Adapter\WrapperInterface'); - $this->factory->registerWrapper('test', $wrapper); - } - - private function getAdapterMock() - { - return $this->getMockBuilder('Phinx\Db\Adapter\AdapterInterface')->getMock(); - } - - public function testGetWrapper() - { - $wrapper = $this->factory->getWrapper('timed', $this->getAdapterMock()); - - $this->assertInstanceOf(TimedOutputAdapter::class, $wrapper); - } - - public function testGetWrapperFailure() - { - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('Wrapper "nope" has not been registered'); - $this->factory->getWrapper('nope', $this->getAdapterMock()); - } }