2010-12-24 19:37:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a set of utility functions for managing migrations
|
|
|
|
*
|
|
|
|
* @author Matt Button <matthew@sigswitch.com>
|
|
|
|
**/
|
|
|
|
class Minion_Migration_Util {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a set of files generated by Kohana::find_files and compiles it
|
|
|
|
* down into an array of migrations
|
|
|
|
*
|
|
|
|
* @param array Available files
|
|
|
|
* @return array Available Migrations
|
|
|
|
*/
|
|
|
|
public static function compile_migrations_from_files(array $files)
|
|
|
|
{
|
|
|
|
$migrations = array();
|
|
|
|
|
|
|
|
foreach($files as $file => $path)
|
|
|
|
{
|
|
|
|
// If this is a directory we're dealing with
|
|
|
|
if(is_array($path))
|
|
|
|
{
|
|
|
|
$migrations += Minion_Migration_Util::compile_migrations_from_files($path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-29 01:23:01 +02:00
|
|
|
$migration = Minion_Migration_Util::get_migration_from_filename($file);
|
2010-12-24 19:37:52 +02:00
|
|
|
|
2010-12-29 01:23:01 +02:00
|
|
|
$migrations[$migration['id']] = array(
|
|
|
|
'file' => $file,
|
|
|
|
'location' => $migration['location']
|
|
|
|
);
|
2010-12-24 19:37:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $migrations;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts information about a migration from its filename.
|
|
|
|
*
|
|
|
|
* Returns an array like:
|
|
|
|
*
|
|
|
|
* array(
|
2010-12-28 04:47:49 +02:00
|
|
|
* 'location' => 'mylocation',
|
2010-12-24 19:37:52 +02:00
|
|
|
* 'id' => '1293214439_initial-setup',
|
2010-12-28 04:47:49 +02:00
|
|
|
* 'file' => 'migrations/mylocation/1293214439_initial-setup.php',
|
2010-12-24 19:37:52 +02:00
|
|
|
* 'timestamp' => '1293214439',
|
|
|
|
* 'description' => 'initial-setup',
|
|
|
|
* );
|
|
|
|
*
|
|
|
|
* @param string The migration's filename
|
|
|
|
* @return array Array of components about the migration
|
|
|
|
*/
|
2010-12-29 01:23:01 +02:00
|
|
|
public static function get_migration_from_filename($file)
|
2010-12-24 19:37:52 +02:00
|
|
|
{
|
|
|
|
$migration = array();
|
|
|
|
|
|
|
|
// Get rid of the file's "migrations/" prefix, the file extension and then
|
2010-12-28 04:47:49 +02:00
|
|
|
// the filename itself. The "location" is essentially a slash delimited
|
2010-12-24 19:37:52 +02:00
|
|
|
// path from the migrations folder to the migration file
|
2010-12-28 04:47:49 +02:00
|
|
|
$migration['location'] = dirname(substr($file, 11, -strlen(EXT)));
|
2010-12-24 19:37:52 +02:00
|
|
|
$migration['id'] = basename($file, EXT);
|
|
|
|
$migration['file'] = $file;
|
|
|
|
|
|
|
|
list($migration['timestamp'], $migration['description'])
|
|
|
|
= explode('_', $migration['id'], 2);
|
|
|
|
|
|
|
|
return $migration;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-12-28 04:47:49 +02:00
|
|
|
* Gets a migration file from its timestamp, description and location
|
2010-12-24 19:37:52 +02:00
|
|
|
*
|
|
|
|
* @param integer|array The migration's ID or an array of timestamp, description
|
2010-12-28 04:47:49 +02:00
|
|
|
* @param string The migration location
|
2010-12-24 19:37:52 +02:00
|
|
|
* @return string Path to the migration file
|
|
|
|
*/
|
2010-12-29 01:23:01 +02:00
|
|
|
public static function get_filename_from_migration($migration, $location)
|
2010-12-24 19:37:52 +02:00
|
|
|
{
|
|
|
|
if(is_array($migration))
|
|
|
|
{
|
2010-12-29 01:23:01 +02:00
|
|
|
$location = $migration['location'];
|
|
|
|
$migration = $migration['id'];
|
2010-12-24 19:37:52 +02:00
|
|
|
}
|
|
|
|
|
2010-12-28 04:47:49 +02:00
|
|
|
$location = ! empty($location) ? rtrim($location, '/').'/' : '';
|
2010-12-24 19:37:52 +02:00
|
|
|
|
2010-12-29 01:23:01 +02:00
|
|
|
return $location.$migration.EXT;
|
2010-12-24 19:37:52 +02:00
|
|
|
}
|
|
|
|
}
|