1
0
Fork 0
mirror of https://github.com/Oreolek/kohana-migrations.git synced 2024-07-01 06:05:05 +03:00
kohana-migrations/classes/minion/migration/util.php

96 lines
2.5 KiB
PHP

<?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
{
$migration = Minion_Migration_Util::get_migration_from_filename($file);
$migrations[$migration['id']] = array(
'file' => $file,
'location' => $migration['location']
);
}
}
return $migrations;
}
/**
* Extracts information about a migration from its filename.
*
* Returns an array like:
*
* array(
* 'location' => 'mylocation',
* 'id' => '1293214439_initial-setup',
* 'file' => 'migrations/mylocation/1293214439_initial-setup.php',
* 'timestamp' => '1293214439',
* 'description' => 'initial-setup',
* );
*
* @param string The migration's filename
* @return array Array of components about the migration
*/
public static function get_migration_from_filename($file)
{
$migration = array();
// Get rid of the file's "migrations/" prefix, the file extension and then
// the filename itself. The "location" is essentially a slash delimited
// path from the migrations folder to the migration file
$migration['location'] = dirname(substr($file, 11, -strlen(EXT)));
$migration['id'] = basename($file, EXT);
$migration['file'] = $file;
list($migration['timestamp'], $migration['description'])
= explode('_', $migration['id'], 2);
return $migration;
}
/**
* Gets a migration file from its timestamp, description and location
*
* @param integer|array The migration's ID or an array of timestamp, description
* @param string The migration location
* @return string Path to the migration file
*/
public static function get_filename_from_migration($migration, $location)
{
if(is_array($migration))
{
$location = $migration['location'];
$migration = $migration['id'];
}
$location = ! empty($location) ? rtrim($location, '/').'/' : '';
return $location.$migration.EXT;
}
}