Skip to content

Commit

Permalink
Merge pull request #36 from larapack/single-files
Browse files Browse the repository at this point in the history
Support single files
  • Loading branch information
marktopper authored Jan 22, 2018
2 parents 8de41bb + 2854f07 commit 6da4b3d
Show file tree
Hide file tree
Showing 4 changed files with 327 additions and 13 deletions.
78 changes: 66 additions & 12 deletions src/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Larapack\Hooks;

use Carbon\Carbon;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Application;
use Symfony\Component\Console\Input\ArrayInput;
Expand Down Expand Up @@ -910,7 +909,13 @@ protected function migrateHook(Hook $hook)
{
$migrations = (array) $hook->getComposerHookKey('migrations', []);

$this->migrator->run($this->realPath($migrations, $hook->getPath().'/')->all());
foreach ($migrations as $path) {
if ($this->filesystem->isDirectory($hook->getPath().'/'.$path)) {
$this->migrator->run($this->realPath([$path], $hook->getPath().'/')->all());
} else {
$this->migrator->runFiles($this->realPath([$path], $hook->getPath().'/')->all());
}
}
}

/**
Expand All @@ -922,7 +927,13 @@ protected function unmigrateHook(Hook $hook)
{
$migrations = (array) $hook->getComposerHookKey('migrations', []);

$this->migrator->reset($this->realPath($migrations, $hook->getPath().'/')->all());
foreach ($migrations as $path) {
if ($this->filesystem->isDirectory($hook->getPath().'/'.$path)) {
$this->migrator->reset($this->realPath([$path], $hook->getPath().'/')->all());
} else {
$this->migrator->resetFiles($this->realPath([$path], $hook->getPath().'/')->all());
}
}
}

/**
Expand Down Expand Up @@ -968,10 +979,17 @@ protected function publishHook(Hook $hook, $force = false)
continue;
}

$allFiles = collect($this->filesystem->allFiles($realLocation))
->map(function ($file) use ($realLocation) {
if ($filesystem->isDirectory($realLocation)) {
$allFiles = collect($filesystem->allFiles($realLocation))->map(function ($file) use ($realLocation) {
return substr($file->getRealPath(), strlen($realLocation) + 1);
});
} else {
$allFiles = collect([new \Symfony\Component\Finder\SplFileInfo(
$realLocation,
'',
basename($realLocation)
)]);
}

$newFiles = $allFiles->filter(function ($filename) use ($publishPath, $filesystem) {
return !$filesystem->exists($publishPath.'/'.$filename);
Expand All @@ -996,6 +1014,16 @@ protected function publishHook(Hook $hook, $force = false)

$newFiles->merge($updatedFiles)
->each(function ($filename) use ($realLocation, $publishPath, $filesystem) {
if (!$filesystem->isDirectory($realLocation)) {
$directory = substr($publishPath, 0, -strlen(basename($publishPath)));

if (!$filesystem->isDirectory($directory)) {
$filesystem->makeDirectory($directory, 0755, true, true);
}

return $filesystem->copy($realLocation, $publishPath);
}

$directory = substr($publishPath.'/'.$filename, 0, -strlen(basename($filename)));

if (!$filesystem->isDirectory($directory)) {
Expand Down Expand Up @@ -1028,17 +1056,33 @@ protected function unpublishHook(Hook $hook)
continue;
}

$allFiles = collect($this->filesystem->allFiles($realLocation))
->map(function ($file) use ($realLocation) {
return substr($file->getRealPath(), strlen($realLocation) + 1);
});
if ($filesystem->isDirectory($realLocation)) {
$allFiles = collect($this->filesystem->allFiles($realLocation))
->map(function ($file) use ($realLocation) {
return substr($file->getRealPath(), strlen($realLocation) + 1);
});
} else {
$allFiles = collect([new \Symfony\Component\Finder\SplFileInfo(
$realLocation,
'',
basename($realLocation)
)]);
}

$existingFiles = $allFiles->filter(function ($filename) use ($publishPath, $filesystem) {
return $filesystem->exists($publishPath.'/'.$filename);
if ($filesystem->isDirectory($publishPath)) {
return $filesystem->exists($publishPath.'/'.$filename);
}

return $filesystem->exists($publishPath);
});

$existingFiles->each(function ($filename) use ($publishPath, $filesystem) {
$filesystem->delete($publishPath.'/'.$filename);
if ($filesystem->isDirectory($publishPath)) {
return $filesystem->delete($publishPath.'/'.$filename);
}

$filesystem->delete($publishPath);
});
}
}
Expand All @@ -1055,7 +1099,17 @@ protected function runSeeders($folders, $basePath)

$this->realPath($folders, $basePath)
->each(function ($folder) use ($filesystem) {
collect($filesystem->files($folder))->filter(function ($file) {
if ($filesystem->isDirectory($folder)) {
$files = $filesystem->files($folder);
} else {
$files = [new \Symfony\Component\Finder\SplFileInfo(
$folder,
'',
basename($folder)
)];
}

collect($files)->filter(function ($file) {
return $file->getExtension() == 'php';
})->each(function ($file) {
$class = substr($file->getFilename(), 0, -4);
Expand Down
12 changes: 11 additions & 1 deletion src/HooksServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,21 @@ public function register()
// Register Hooks system and aliases
$this->app->singleton(Hooks::class, function ($app) {
$filesystem = $app[Filesystem::class];
$migrator = $app['migrator'];
$migrator = $app[Migrator::class];

return new Hooks($filesystem, $migrator);
});

$this->app->alias(Hooks::class, 'hooks');

// The migrator is responsible for actually running and rollback the migration
// files in the application. We'll pass in our database connection resolver
// so the migrator can resolve any of these connections when it needs to.
$this->app->singleton(Migrator::class, function ($app) {
$repository = $app['migration.repository'];

return new Migrator($repository, $app['db'], $app['files']);
});
}

/**
Expand Down
97 changes: 97 additions & 0 deletions src/Migrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Larapack\Hooks;

use Illuminate\Database\Migrations\Migrator as BaseMigrator;
use Illuminate\Support\Arr;

/*
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Database\ConnectionResolverInterface as Resolver;
*/

class Migrator extends BaseMigrator
{
public function runFiles(array $files, array $options = [])
{
$this->notes = [];

$this->requireFiles($migrations = $this->pendingMigrations(
$files, $this->repository->getRan()
));

// Once we have all these migrations that are outstanding we are ready to run
// we will go ahead and run them "up". This will execute each migration as
// an operation against a database. Then we'll return this list of them.
$this->runPending($migrations, $options);

return $migrations;
}

public function resetFiles(array $files = [], $pretend = false)
{
$this->notes = [];

$files = collect($files)->keyBy(function ($file) {
return rtrim(basename($file), '.php');
})->all();

// Next, we will reverse the migration list so we can run them back in the
// correct order for resetting this database. This will allow us to get
// the database back into its "empty" state ready for the migrations.
$migrations = array_reverse($this->repository->getRan());

if (count($migrations) === 0) {
$this->note('<info>Nothing to rollback.</info>');

return [];
}

return $this->resetMigrationsByFiles($migrations, $files, $pretend);
}

protected function resetMigrationsByFiles(array $migrations, array $files, $pretend = false)
{
// Since the getRan method that retrieves the migration name just gives us the
// migration name, we will format the names into objects with the name as a
// property on the objects so that we can pass it to the rollback method.
$migrations = collect($migrations)->map(function ($m) {
return (object) ['migration' => $m];
})->all();

return $this->rollbackMigrationsByFiles(
$migrations, $files, compact('pretend')
);
}

protected function rollbackMigrationsByFiles(array $migrations, $files, array $options)
{
$rolledBack = [];

$this->requireFiles($files);

// Next we will run through all of the migrations and call the "down" method
// which will reverse each migration in order. This getLast method on the
// repository already returns these migration's names in reverse order.
foreach ($migrations as $migration) {
$migration = (object) $migration;

if (!$file = Arr::get($files, $migration->migration)) {
$this->note("<fg=red>Migration not found:</> {$migration->migration}");

continue;
}

$rolledBack[] = $file;

$this->runDown(
$file, $migration,
$options['pretend'] ?? false
);
}

return $rolledBack;
}
}
Loading

0 comments on commit 6da4b3d

Please sign in to comment.