Skip to content

Commit

Permalink
ENHANCEMENT Ensure resources folder has .htaccess created
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Mooyman committed Nov 9, 2017
1 parent 0401347 commit f61e01b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
5 changes: 5 additions & 0 deletions resources/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Block 404s
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* - [R=404,L]
</IfModule>
53 changes: 48 additions & 5 deletions src/VendorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Composer\Package\PackageInterface;
use Composer\Plugin\PluginInterface;
use Composer\Util\Filesystem;
use DirectoryIterator;
use SilverStripe\VendorPlugin\Methods\CopyMethod;
use SilverStripe\VendorPlugin\Methods\ExposeMethod;
use SilverStripe\VendorPlugin\Methods\ChainedMethod;
Expand Down Expand Up @@ -43,6 +44,16 @@ class VendorPlugin implements PluginInterface, EventSubscriberInterface
*/
const METHOD_AUTO = 'auto';

/**
* @var Filesystem
*/
protected $filesystem = null;

public function __construct()
{
$this->filesystem = new Filesystem();
}

/**
* Apply vendor plugin
*
Expand Down Expand Up @@ -77,7 +88,7 @@ protected function getVendorModule(PackageEvent $event)
}

// Find project path
$projectPath = dirname(realpath(Factory::getComposerFile()));
$projectPath = $this->getProjectPath();
$name = $package->getName();

// Build module
Expand Down Expand Up @@ -110,11 +121,44 @@ public function installPackage(PackageEvent $event)
$event->getIO()->write(" - <info>$folder</info>");
}

// Setup root folder
$this->setupResources();

// Expose web dirs with given method
$method = $this->getMethod();
$module->exposePaths($method);
}

/**
* @return string
*/
protected function getProjectPath()
{
return dirname(realpath(Factory::getComposerFile()));
}

/**
* Ensure the resources folder is safely created and protected from index.php in root
*/
protected function setupResources()
{
// Setup root dir
$resourcesPath = Util::joinPaths(
$this->getProjectPath(),
VendorModule::DEFAULT_TARGET
);
$this->filesystem->ensureDirectoryExists($resourcesPath);

// Copy missing resources
$files = new DirectoryIterator(__DIR__.'/../resources');
foreach ($files as $file) {
$targetPath = $resourcesPath . DIRECTORY_SEPARATOR . $file->getFilename();
if ($file->isFile() && !file_exists($targetPath)) {
copy($file->getPathname(), $targetPath);
}
}
}

/**
* Remove package
*
Expand All @@ -130,20 +174,19 @@ public function uninstallPackage(PackageEvent $event)

// Check path to remove
$target = $module->getModulePath(VendorModule::DEFAULT_TARGET);
$filesystem = new Filesystem();
if (!is_dir($target)) {
return;
}

// Remove directory
$name = $module->getName();
$event->getIO()->write("Removing web directories for module <info>{$name}</info>:");
$filesystem->removeDirectory($target);
$this->filesystem->removeDirectory($target);

// Cleanup empty vendor dir if this is the last module
$vendorTarget = dirname($target);
if ($filesystem->isDirEmpty($vendorTarget)) {
$filesystem->removeDirectory($vendorTarget);
if ($this->filesystem->isDirEmpty($vendorTarget)) {
$this->filesystem->removeDirectory($vendorTarget);
}
}

Expand Down

0 comments on commit f61e01b

Please sign in to comment.