Skip to content

Instantly share code, notes, and snippets.

@brianium
Created March 28, 2017 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianium/1e854980e57bad365f723b0267569a74 to your computer and use it in GitHub Desktop.
Save brianium/1e854980e57bad365f723b0267569a74 to your computer and use it in GitHub Desktop.
<?php
use Phinx\Db\Adapter\AdapterFactory;
use Phinx\Migration\Manager;
use Phinx\Migration\MigrationInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
class WordPressManager extends Manager implements MigratorInterface
{
/**
* @var \wpdb
*/
protected $db;
/**
* @var BufferedOutput
*/
protected $output;
/**
* @var Container
*/
protected $container;
/**
* @param \wpdb $wpdb
*/
public function __construct(\wpdb $wpdb)
{
// global $wpdb can be used instead of injecting it if preferred
$output = new BufferedOutput();
$this->db = $wpdb;
$config = $this->createWordPressConfiguration();
$factory = AdapterFactory::instance();
$factory->registerAdapter('wpdb', 'WpdbAdapter');
parent::__construct($config, $output);
}
/**
* Return the contents of the buffered output
*
* @return string
*/
public function getBufferedOutput()
{
return $this->output->fetch();
}
/**
* Return a configuration constructed using the WordPress database and DB constants
*
* @return Config
*/
protected function createWordPressConfiguration()
{
$prefix = $this->db->prefix;
$configArray = [
//'migration_base_class' => 'AbstractBaseMigration', can provide a base migration if you need to
'environments' => [
'default_migration_table' => "{$prefix}myradplugin_migrations",
'default_database' => 'production',
'production' => [
'adapter' => 'wpdb',
'host' => DB_HOST,
'name' => DB_NAME,
'user' => DB_USER,
'pass' => DB_PASSWORD,
'charset' => DB_CHARSET,
'collation' => $this->db->get_charset_collate()
]
]
];
return new Config($configArray);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment