Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .travis/test-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,41 @@ if [ ! -L "$SITE_WEB/themes/travis-test-image.jpg" ]; then
else
echo "${MSG_OK} 'composer drupal:paranoia' command re-created the web directory with new symlinks"
fi

##
# Create a "customfile.txt", configure the 'drupal-asset-files' extra key,
# run the command 'composer drupal:paranoia' and check if the file has been symlinked.
#
echo "${MSG_INFO} Create a \"customfile.txt\" and configure the 'drupal-asset-files' extra key to check if the file has been symlinked."

touch "$SITE_APP/customfile.txt"
composer config extra.drupal-asset-files.should-be-simlinked customfile.txt

# Rebuild web directory.
composer drupal:paranoia || exit 1

if [ ! -L "$SITE_WEB/customfile.txt" ]; then
echo "${MSG_ERROR} 'composer drupal:paranoia' command did not re-create the web directory with extra symlinks"
exit 1
else
echo "${MSG_OK} 'composer drupal:paranoia' command re-created the web directory with extra symlinks"
fi

##
# Create a "customfile.php", configure the 'drupal-asset-files' extra key,
# run the command 'composer drupal:paranoia' and check if the file has not been symlinked.
#
echo "${MSG_INFO} Create a \"customfile.php\" and configure the 'drupal-asset-files' extra key to check if the file has not been symlinked."

touch "$SITE_APP/customfile.php"
composer config extra.drupal-asset-files.should-not-be-simlinked customfile.php

# Rebuild web directory.
composer drupal:paranoia || exit 1

if [ -L "$SITE_WEB/customfile.php" ]; then
echo "${MSG_ERROR} 'composer drupal:paranoia' command re-created the web directory with WRONG extra symlinks"
exit 1
else
echo "${MSG_OK} 'composer drupal:paranoia' command did not re-create the web directory with WRONG extra symlinks"
fi
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,56 @@ composer require drupal-composer/drupal-paranoia:~1

Done! Plugin and new docroot are now installed.

## Optional Configuration

### Modify the asset file types

To extend the list of assets file types you can use the
`drupal-asset-files` extra key:
```json
"extra": {
"drupal-asset-files": [
"somefile.txt",
"*.md"
],
"..."
}
```

If you need to modify it you can use the
`post-drupal-set-asset-file-types` event:
```json
"scripts": {
"post-drupal-set-asset-file-types": [
"DrupalProject\\composer\\ScriptHandler::setAssetFileTypes"
],
"..."
},
```

```php
<?php

/**
* @file
* Contains \DrupalProject\composer\ScriptHandler.
*/

namespace DrupalProject\composer;

use DrupalComposer\DrupalParanoia\AssetFileTypesEvent;

class ScriptHandler {

public static function setAssetFileTypes(AssetFileTypesEvent $event) {
$asset_file_types = $event->getAssetFileTypes();
// Do what you want with the asset file types.
$event->setAssetFileTypes($asset_file_types);
}

}
```

## Folder structure
Your project now is basically structured on two folders.
- __app__: Contains the files and folders of the full Drupal installation.
Expand Down
94 changes: 94 additions & 0 deletions src/AssetFileTypesEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace DrupalComposer\DrupalParanoia;

use Composer\Composer;
use Composer\EventDispatcher\Event;
use Composer\IO\IOInterface;

/**
* Class AssetFileTypesEvent.
*
* @package DrupalComposer\DrupalParanoia
*/
class AssetFileTypesEvent extends Event {

/**
* This event occurs after the asset file types have been set.
*
* @var string
*/
const POST_DRUPAL_SET_ASSET_FILE_TYPES = 'post-drupal-set-asset-file-types';

/**
* Composer object.
*
* @var \Composer\Composer
*/
private $composer;

/**
* IO object.
*
* @var \Composer\IO\IOInterface
*/
private $io;

/**
* Asset file types.
*
* @var array
*/
private $assetFileTypes;

/**
* AssetFileTypesEvent constructor.
*
* @param array $assetFileTypes
* The asset file types.
* @param \Composer\Composer $composer
* The composer object.
* @param \Composer\IO\IOInterface $io
* The IOInterface object.
* @param array $args
* Arguments passed by the user
* @param array $flags
* Optional flags to pass data not as argument.
*/
public function __construct(array $assetFileTypes, Composer $composer, IOInterface $io, array $args = array(), array $flags = array()) {
parent::__construct(self::POST_DRUPAL_SET_ASSET_FILE_TYPES, $args, $flags);

$this->composer = $composer;
$this->io = $io;
$this->assetFileTypes = $assetFileTypes;
}

/**
* @return \Composer\Composer
*/
public function getComposer() {
return $this->composer;
}

/**
* @return \Composer\IO\IOInterface
*/
public function getIo() {
return $this->io;
}

/**
* @return array
*/
public function getAssetFileTypes() {
return $this->assetFileTypes;
}

/**
* @param array $assetFileTypes
*/
public function setAssetFileTypes(array $assetFileTypes) {
$this->assetFileTypes = $assetFileTypes;
}

}
64 changes: 47 additions & 17 deletions src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
*/
class Installer {

/**
* Composer object.
*
* @var \Composer\Composer
*/
protected $composer;

/**
* IO object.
*
Expand All @@ -32,22 +39,7 @@ class Installer {
*
* @var array
*/
public $assetFileTypes = [
'.htaccess',
'*.css',
'*.eot',
'*.ico',
'*.gif',
'*.jpeg',
'*.jpg',
'*.js',
'*.otf',
'*.png',
'*.svg',
'*.ttf',
'*.woff',
'*.woff2',
];
public $assetFileTypes;

/**
* Front controllers.
Expand Down Expand Up @@ -98,9 +90,10 @@ class Installer {
* The IO object.
*/
public function __construct(Composer $composer, IOInterface $io) {
$this->composer = $composer;
$this->io = $io;

$extra = $composer->getPackage()->getExtra();
$extra = $this->composer->getPackage()->getExtra();

if (!isset($extra['drupal-app-dir'])) {
throw new \RuntimeException('Please configure drupal-app-dir in your composer.json');
Expand All @@ -117,6 +110,8 @@ public function __construct(Composer $composer, IOInterface $io) {
if (!empty($extra['drupal-web-dir-public-files'])) {
$this->publicFilesSymlinkTarget = $extra['drupal-web-dir-public-files'];
}

$this->setAssetFileTypes();
}

/**
Expand Down Expand Up @@ -262,6 +257,7 @@ public function createAssetSymlinks() {
$finder->name($name);
}
$finder->exclude('sites/default/files');
$finder->notName('/\.php/');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I've extended the list to:

+ $finder->notName('*.install'); + $finder->notName('*.module'); + $finder->notName('*.php'); + $finder->notName('*.profile'); + $finder->notName('*.theme');``` https://github.com/drupal-composer/drupal-paranoia/commit/160a9337b91224eecf3e9671480a3cb1381e457c#diff-c984225cadcacb6a40cb298c78b32434R260 

$cfs = new ComposerFilesystem();

Expand Down Expand Up @@ -299,4 +295,38 @@ public function createStubPhpFile($path) {
$fs->dumpFile($webDir . '/' . $path, $content);
}

/**
* Set the asset file types.
*/
public function setAssetFileTypes() {
$this->assetFileTypes = [
'robots.txt',
'.htaccess',
'*.css',
'*.eot',
'*.ico',
'*.gif',
'*.jpeg',
'*.jpg',
'*.js',
'*.otf',
'*.png',
'*.svg',
'*.ttf',
'*.woff',
'*.woff2',
];

// Allow people to extend the list from a composer extra key.
$extra = $this->composer->getPackage()->getExtra();
if(!empty($extra['drupal-asset-files'])) {
$this->assetFileTypes = array_merge($this->assetFileTypes, $extra['drupal-asset-files']);
}

// Allow other plugins to alter the list of files.
$event = new AssetFileTypesEvent($this->assetFileTypes, $this->composer, $this->io);
$this->composer->getEventDispatcher()->dispatch($event->getName(), $event);
$this->assetFileTypes = $event->getAssetFileTypes();
}

}