Skip to content

Commit

Permalink
fixed #173
Browse files Browse the repository at this point in the history
  • Loading branch information
bupy7 committed Aug 24, 2017
1 parent 1f0dbcb commit 674632c
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 20 deletions.
3 changes: 2 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Below are the description of configuration options in the main `assetic_configur
| webPath | string | `public/assets` | Here, all assets will be saved
| cachePath | string | `data/cache` | Here, cache metadata will be saved
| cacheEnabled | boolean | `true` | If, true cache will be used on assets using filters. This is very useful if we use filters like scss, closure,...
| umask | integer | `null` | Yes, is regular `umask` apply on generated assets
| filePermission | integer | `null` | Permission mode of `chmod` command in octal system (exampe: 0777 or 0750) for files.
| dirPermission | integer | `null` | Permission mode of `chmod` and `mkdir` command in octal system (exampe: 0777 or 0750) for directories.
| baseUrl | string | `null` | Define base URL which will prepend your resources. If `null`, then this value will be detected by ZF2
| basePath | string | `assets` | Indicate where assets are and from where will be loaded. In example where `$baseUrl = 'http://example.com/'` `$basePath = 'assets'` `$assetPath = '/main.css'` view strategy will build such resource address `<link href="$baseUrl . $basePath . $assetPath"/>`
| controllers | array | - | Described in separate section
Expand Down
6 changes: 5 additions & 1 deletion src/AsseticBundle/Cli/SetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public function __construct(Service $assetic)
protected function execute(InputInterface $input, OutputInterface $output)
{
$config = $this->assetic->getConfiguration();
$mode = (null !== ($mode = $config->getUmask())) ? $mode : 0775;

$mode = $config->getDirPermission();
if ($mode === null) {
$mode = 0775;
}

if (!$this->createPath($output, 'Cache', $config->getCachePath(), $mode)) {
return 1;
Expand Down
44 changes: 33 additions & 11 deletions src/AsseticBundle/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,14 @@ class Configuration
protected $acceptableErrors = [];

/**
* Umask
*
* @var null|int
* @var null|int File permission for assetic files.
*/
protected $filePermission = null;

/**
* @var null|int Directory permission for assetic directories.
*/
protected $umask = null;
protected $dirPermission = null;

public function __construct($config = null)
{
Expand Down Expand Up @@ -394,19 +397,38 @@ public function getAcceptableErrors()
/**
* @return int|null
*/
public function getUmask()
public function getFilePermission()
{
return $this->filePermission;
}

/**
* @param null|int $filePermission
*/
public function setFilePermission($filePermission)
{
$this->filePermission = null;
if (is_int($filePermission)) {
$this->filePermission = (int) $filePermission;
}
}

/**
* @return int|null
*/
public function getDirPermission()
{
return $this->umask;
return $this->dirPermission;
}

/**
* @param null|int $umask
* @param null|int $dirPermission
*/
public function setUmask($umask)
public function setDirPermission($dirPermission)
{
$this->umask = null;
if (is_int($umask)) {
$this->umask = (int) $umask;
$this->dirPermission = null;
if (is_int($dirPermission)) {
$this->dirPermission = (int) $dirPermission;
}
}

Expand Down
38 changes: 31 additions & 7 deletions src/AsseticBundle/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,23 +525,47 @@ public function writeAsset(AssetInterface $asset, Factory\AssetFactory $factory)
*/
protected function write(AssetInterface $asset, Factory\AssetFactory $factory)
{
$umask = $this->configuration->getUmask();
if (null !== $umask) {
$umask = umask($umask);
}

if ($this->configuration->isDebug() && !$this->configuration->isCombine()
&& ($asset instanceof AssetCollection)
) {
/** @var AssetInterface $item */
foreach ($asset as $item) {
$this->writeAsset($item, $factory);
}
} else {
$this->getAssetWriter()->writeAsset($asset);
}
$this->setPermission($asset);
}

/**
* @param AssetInterface $asset Asset was wrote
*/
protected function setPermission(AssetInterface $asset)
{
$target = $this->configuration->getWebPath($asset->getTargetPath());

if (is_file($target)) {
if ($this->configuration->getFilePermission() !== null) {
chmod($target, $this->configuration->getFilePermission());
}
$baseDir = dirname($asset->getTargetPath());
} else {
$baseDir = $asset->getTargetPath();
}

if ($this->configuration->getDirPermission() === null) {
return;
}

$dirNames = explode('/', rtrim($baseDir, '/'));

if (null !== $umask) {
umask($umask);
$dPerm = $this->configuration->getDirPermission();
$dirName = [];
foreach ($dirNames as $item) {
$dirName[] = $item;
$path = $this->configuration->getWebPath(implode('/', $dirName));
chmod($path, $dPerm);
}
}
}
28 changes: 28 additions & 0 deletions tests/AsseticBundleTest/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,32 @@ public function testAddRendererToStrategy()
$value = $this->object->addRendererToStrategy('a', 'b');
$this->assertNull($value);
}

public function testGetFilePermission()
{
$this->assertNull($this->object->getFilePermission());
}

public function testGetDirPermission()
{
$this->assertNull($this->object->getDirPermission());
}

public function testSetFilePermission()
{
$this->object->setFilePermission('+x');
$this->assertNull($this->object->getFilePermission());

$this->object->setFilePermission(0777);
$this->assertEquals(0777, $this->object->getFilePermission());
}

public function testSetDirPermission()
{
$this->object->setDirPermission('+w');
$this->assertNull($this->object->getDirPermission());

$this->object->setDirPermission(0640);
$this->assertEquals(0640, $this->object->getDirPermission());
}
}
50 changes: 50 additions & 0 deletions tests/AsseticBundleTest/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ protected function setUp()
],
'options' => [],
],
'deeper_base_css' => [
'assets' => [
'css/global.css',
],
'options' => [
'output' => 'chmod/very/deeper/deeper_base_css.css',
],
],
'base_js' => [
'assets' => [
'js/test.js',
Expand Down Expand Up @@ -374,4 +382,46 @@ public function testCacheBusterStrategyWorker()
// cache buster strategy is added to workers list:
$this->assertAttributeEquals([$cacheBusterStrategy], 'workers', $factory);
}

public function testWriteAssetWithSetPermission()
{
$this->configuration->setFilePermission(0777);
$this->configuration->setDirPermission(0770);

$this->object->build();

/** @var \Assetic\Asset\AssetInterface $assets */
$asset = $this->object->getAssetManager()->get('deeper_base_css');
$targetFile = $this->configuration->getWebPath($asset->getTargetPath());

if (is_file($targetFile)) {
unlink($targetFile);
foreach (['chmod/very/deeper', 'chmod/very', 'chmod'] as $name) {
rmdir($this->configuration->getWebPath($name));
}
}

$factory = $this->object->createAssetFactory($this->configuration->getModule('test_application'));

// checking on exists file
$this->assertFileNotExists($targetFile);
$this->object->writeAsset($asset, $factory);
$this->assertFileExists($targetFile);

$filePermisson = function ($path) {
return substr(sprintf('%o', fileperms($path)), -4);
};

// reset cache http://php.net/manual/ru/function.clearstatcache.php
clearstatcache(true, $targetFile);

// checking file permission
$this->assertEquals('0777', $filePermisson($targetFile));

// checking directory permission
foreach (['chmod/very/deeper', 'chmod/very', 'chmod'] as $name) {
$path = $this->configuration->getWebPath($name);
$this->assertEquals('0770', $filePermisson($path));
}
}
}

0 comments on commit 674632c

Please sign in to comment.