/**
  * Get all available migrations for a scope from file system.
  * If direction equals "down" then additionally check against migrated versions in schema_migration
  * to only migrate existing ones down.
  * This makes it possible to insert new migrations in between already existing and migrated ones.
  *
  * @param string $scope The scope (app or plugin name) that you want to get migrations from.
  * @param string $direction The migration direction for which to order the available migrations array.
  * @return array all available migrations for this scope
  */
 protected function _getAvailableMigrations($scope, $direction = 'up')
 {
     if (!isset($this->_migrations[$scope]) || empty($this->_migrations[$scope])) {
         $folder = $this->_getMigrationsFolder($scope);
         if (!$folder) {
             return array();
         }
         $this->_migrations[$scope] = array();
         foreach ($folder->find("^\\d+_.*\\.php\$", true) as $mf) {
             if (preg_match("/^(\\d+)_(.*)\\.php\$/", $mf, $matches) === 1) {
                 list($fileName, $version, $name) = $matches;
                 $this->_migrations[$scope][$version] = array('path' => $folder->path, 'fileName' => $fileName, 'className' => Inflector::camelize($name));
             }
         }
     }
     $migrations = $this->_migrations[$scope];
     if ($direction === 'down') {
         $migrations = array_reverse($migrations);
         $existingMigrations = $this->SchemaMigration->getMigrationsForDown($scope);
         foreach (array_keys($migrations) as $key) {
             if (!array_key_exists($key, $existingMigrations)) {
                 unset($migrations[$key]);
             }
         }
     }
     return $migrations;
 }