Skip to content

Commit

Permalink
Merge pull request #26 from open-sausages/pulls/1.4/configurable-ress…
Browse files Browse the repository at this point in the history
…ources-dir

Make ressource dir configurable
  • Loading branch information
Aaron Carlino authored Dec 3, 2018
2 parents 5cab08d + 0957d8e commit 93b6c53
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 7 deletions.
57 changes: 50 additions & 7 deletions src/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\VendorPlugin;

use Composer\IO\NullIO;
use Composer\Json\JsonFile;
use LogicException;
use SilverStripe\VendorPlugin\Methods\ExposeMethod;
Expand All @@ -18,10 +19,16 @@ class Library
*/
const PUBLIC_PATH = 'public';

/**
* Default folder where vendor resources will be exposed.
*/
const DEFAULT_RESOURCES_DIR = 'resources';

/**
* Subfolder to map within public webroot
* @deprecated 1.4.0..2.0.0 Use Library::getResourcesDir() instead
*/
const RESOURCES_PATH = 'resources';
const RESOURCES_PATH = self::DEFAULT_RESOURCES_DIR;

/**
* Project root
Expand All @@ -44,8 +51,11 @@ class Library
* @param string $libraryPath Path to this library
* @param string $name Composer name of this library
*/
public function __construct($basePath, $libraryPath, $name = null)
{
public function __construct(
$basePath,
$libraryPath,
$name = null
) {
$this->basePath = realpath($basePath);
$this->path = realpath($libraryPath);
$this->name = $name;
Expand All @@ -70,6 +80,7 @@ public function getName()
}
// Get from composer
$json = $this->getJson();

if (isset($json['name'])) {
$this->name = $json['name'];
}
Expand Down Expand Up @@ -104,14 +115,15 @@ public function getBasePath()
/**
* Get base path to expose all libraries to
*
* @return string Path with no trailing slash E.g. /var/www/public/resources
* @return string Path with no trailing slash E.g. /var/www/public/_resources
*/
public function getBasePublicPath()
{
$projectPath = $this->getBasePath();
$resourceDir = $this->getResourcesDir();
$publicPath = $this->publicPathExists()
? Util::joinPaths($projectPath, self::PUBLIC_PATH, self::RESOURCES_PATH)
: Util::joinPaths($projectPath, self::RESOURCES_PATH);
? Util::joinPaths($projectPath, self::PUBLIC_PATH, $resourceDir)
: Util::joinPaths($projectPath, $resourceDir);
return $publicPath;
}

Expand Down Expand Up @@ -140,7 +152,7 @@ public function getRelativePath()
/**
* Get base path to map resources for this module
*
* @return string Path with trimmed slashes. E.g. /var/www/public/resources/vendor/silverstripe/module
* @return string Path with trimmed slashes. E.g. /var/www/public/_resources/vendor/silverstripe/module
*/
public function getPublicPath()
{
Expand Down Expand Up @@ -280,4 +292,35 @@ protected function installedIntoVendor()
{
return preg_match('#^vendor[/\\\\]#', $this->getRelativePath());
}

/**
* Determine the name of the folder where vendor module's resources will be exposed. e.g. `_resources`
* @throws LogicException
* @return string
*/
public function getResourcesDir()
{
$rootComposerFile = $this->getBasePath() . '/composer.json';
$rootProject = new JsonFile($rootComposerFile, null, new NullIO());

if (!$rootProject->exists()) {
return self::DEFAULT_RESOURCES_DIR;
}

$rootProjectData = $rootProject->read();
$resourcesDir = isset($rootProjectData['extra']['resources-dir'])
? $rootProjectData['extra']['resources-dir']
: self::DEFAULT_RESOURCES_DIR;


if (preg_match('/^[_\-a-z0-9]+$/i', $resourcesDir)) {
return $resourcesDir;
}

throw new LogicException(sprintf(
'Resources dir error: "%s" is not a valid resources directory name. Update the ' .
'`extra.resources-dir` key in your composer.json file',
$resourcesDir
));
}
}
36 changes: 36 additions & 0 deletions tests/LibraryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace SilverStripe\VendorPlugin\Tests\Methods;

use PHPUnit\Framework\TestCase;
use SilverStripe\VendorPlugin\Library;

class LibraryTest extends TestCase
{
/**
* @dataProvider resourcesDirProvider
*/
public function testResourcesDir($expected, $projectPath)
{
$path = __DIR__ . '/fixtures/projects/' . $projectPath;
$lib = new Library($path, 'vendor/silverstripe/skynet');
$this->assertEquals($expected, $lib->getResourcesDir());
}

public function resourcesDirProvider()
{
return [
['resources', 'ss43'],
['_resources', 'ss44'],
['customised-resources-dir', 'ss44WithCustomResourcesDir']
];
}

public function testInvalidResourceDir()
{
$this->expectException(\LogicException::class);
$path = __DIR__ . '/fixtures/projects/ss44InvalidResourcesDir';
$lib = new Library($path, 'vendor/silverstripe/skynet');
$lib->getResourcesDir();
}
}
28 changes: 28 additions & 0 deletions tests/fixtures/projects/ss43/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "silverstripe/ss42",
"type": "silverstripe-project",
"description": "Fake project using SS 4.3",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.3.x-dev as 4.3.0"
},
"extra": {
"project-files-installed": [
"app/.htaccess",
"app/_config.php",
"app/_config/mysite.yml",
"app/src/Page.php",
"app/src/PageController.php"
],
"public-files-installed": [
".htaccess",
"index.php",
"install-frameworkmissing.html",
"install.php",
"web.config"
]
},
"prefer-stable": true,
"minimum-stability": "dev"
}
29 changes: 29 additions & 0 deletions tests/fixtures/projects/ss44/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "silverstripe/ss44",
"type": "silverstripe-project",
"description": "Fake project using SS 4.4",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"extra": {
"project-files-installed": [
"app/.htaccess",
"app/_config.php",
"app/_config/mysite.yml",
"app/src/Page.php",
"app/src/PageController.php"
],
"public-files-installed": [
".htaccess",
"index.php",
"install-frameworkmissing.html",
"install.php",
"web.config"
],
"resources-dir": "_resources"
},
"prefer-stable": true,
"minimum-stability": "dev"
}
29 changes: 29 additions & 0 deletions tests/fixtures/projects/ss44InvalidResourcesDir/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "silverstripe/ss44",
"type": "silverstripe-project",
"description": "Fake project using SS 4.4",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"extra": {
"project-files-installed": [
"app/.htaccess",
"app/_config.php",
"app/_config/mysite.yml",
"app/src/Page.php",
"app/src/PageController.php"
],
"public-files-installed": [
".htaccess",
"index.php",
"install-frameworkmissing.html",
"install.php",
"web.config"
],
"resources-dir": "Vendor plugin will throw a !$%^#$!#$!&^# fit!!!"
},
"prefer-stable": true,
"minimum-stability": "dev"
}
29 changes: 29 additions & 0 deletions tests/fixtures/projects/ss44WithCustomResourcesDir/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "silverstripe/ss44",
"type": "silverstripe-project",
"description": "Fake project using SS 4.4",
"homepage": "https://www.silverstripe.org",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "4.4.x-dev as 4.4.0"
},
"extra": {
"project-files-installed": [
"app/.htaccess",
"app/_config.php",
"app/_config/mysite.yml",
"app/src/Page.php",
"app/src/PageController.php"
],
"public-files-installed": [
".htaccess",
"index.php",
"install-frameworkmissing.html",
"install.php",
"web.config"
],
"resources-dir": "customised-resources-dir"
},
"prefer-stable": true,
"minimum-stability": "dev"
}

0 comments on commit 93b6c53

Please sign in to comment.