Skip to content

Commit e61b0db

Browse files
authored
Merge pull request #174 from bupy7/issue-173
fixed #173
2 parents 1f0dbcb + 674632c commit e61b0db

File tree

6 files changed

+149
-20
lines changed

6 files changed

+149
-20
lines changed

docs/config.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ Below are the description of configuration options in the main `assetic_configur
1414
| webPath | string | `public/assets` | Here, all assets will be saved
1515
| cachePath | string | `data/cache` | Here, cache metadata will be saved
1616
| cacheEnabled | boolean | `true` | If, true cache will be used on assets using filters. This is very useful if we use filters like scss, closure,...
17-
| umask | integer | `null` | Yes, is regular `umask` apply on generated assets
17+
| filePermission | integer | `null` | Permission mode of `chmod` command in octal system (exampe: 0777 or 0750) for files.
18+
| dirPermission | integer | `null` | Permission mode of `chmod` and `mkdir` command in octal system (exampe: 0777 or 0750) for directories.
1819
| baseUrl | string | `null` | Define base URL which will prepend your resources. If `null`, then this value will be detected by ZF2
1920
| 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"/>`
2021
| controllers | array | - | Described in separate section

src/AsseticBundle/Cli/SetupCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ public function __construct(Service $assetic)
3939
protected function execute(InputInterface $input, OutputInterface $output)
4040
{
4141
$config = $this->assetic->getConfiguration();
42-
$mode = (null !== ($mode = $config->getUmask())) ? $mode : 0775;
42+
43+
$mode = $config->getDirPermission();
44+
if ($mode === null) {
45+
$mode = 0775;
46+
}
4347

4448
if (!$this->createPath($output, 'Cache', $config->getCachePath(), $mode)) {
4549
return 1;

src/AsseticBundle/Configuration.php

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,14 @@ class Configuration
131131
protected $acceptableErrors = [];
132132

133133
/**
134-
* Umask
135-
*
136-
* @var null|int
134+
* @var null|int File permission for assetic files.
135+
*/
136+
protected $filePermission = null;
137+
138+
/**
139+
* @var null|int Directory permission for assetic directories.
137140
*/
138-
protected $umask = null;
141+
protected $dirPermission = null;
139142

140143
public function __construct($config = null)
141144
{
@@ -394,19 +397,38 @@ public function getAcceptableErrors()
394397
/**
395398
* @return int|null
396399
*/
397-
public function getUmask()
400+
public function getFilePermission()
401+
{
402+
return $this->filePermission;
403+
}
404+
405+
/**
406+
* @param null|int $filePermission
407+
*/
408+
public function setFilePermission($filePermission)
409+
{
410+
$this->filePermission = null;
411+
if (is_int($filePermission)) {
412+
$this->filePermission = (int) $filePermission;
413+
}
414+
}
415+
416+
/**
417+
* @return int|null
418+
*/
419+
public function getDirPermission()
398420
{
399-
return $this->umask;
421+
return $this->dirPermission;
400422
}
401423

402424
/**
403-
* @param null|int $umask
425+
* @param null|int $dirPermission
404426
*/
405-
public function setUmask($umask)
427+
public function setDirPermission($dirPermission)
406428
{
407-
$this->umask = null;
408-
if (is_int($umask)) {
409-
$this->umask = (int) $umask;
429+
$this->dirPermission = null;
430+
if (is_int($dirPermission)) {
431+
$this->dirPermission = (int) $dirPermission;
410432
}
411433
}
412434

src/AsseticBundle/Service.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -525,23 +525,47 @@ public function writeAsset(AssetInterface $asset, Factory\AssetFactory $factory)
525525
*/
526526
protected function write(AssetInterface $asset, Factory\AssetFactory $factory)
527527
{
528-
$umask = $this->configuration->getUmask();
529-
if (null !== $umask) {
530-
$umask = umask($umask);
531-
}
532-
533528
if ($this->configuration->isDebug() && !$this->configuration->isCombine()
534529
&& ($asset instanceof AssetCollection)
535530
) {
531+
/** @var AssetInterface $item */
536532
foreach ($asset as $item) {
537533
$this->writeAsset($item, $factory);
538534
}
539535
} else {
540536
$this->getAssetWriter()->writeAsset($asset);
541537
}
538+
$this->setPermission($asset);
539+
}
540+
541+
/**
542+
* @param AssetInterface $asset Asset was wrote
543+
*/
544+
protected function setPermission(AssetInterface $asset)
545+
{
546+
$target = $this->configuration->getWebPath($asset->getTargetPath());
547+
548+
if (is_file($target)) {
549+
if ($this->configuration->getFilePermission() !== null) {
550+
chmod($target, $this->configuration->getFilePermission());
551+
}
552+
$baseDir = dirname($asset->getTargetPath());
553+
} else {
554+
$baseDir = $asset->getTargetPath();
555+
}
556+
557+
if ($this->configuration->getDirPermission() === null) {
558+
return;
559+
}
560+
561+
$dirNames = explode('/', rtrim($baseDir, '/'));
542562

543-
if (null !== $umask) {
544-
umask($umask);
563+
$dPerm = $this->configuration->getDirPermission();
564+
$dirName = [];
565+
foreach ($dirNames as $item) {
566+
$dirName[] = $item;
567+
$path = $this->configuration->getWebPath(implode('/', $dirName));
568+
chmod($path, $dPerm);
545569
}
546570
}
547571
}

tests/AsseticBundleTest/Configuration.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,32 @@ public function testAddRendererToStrategy()
320320
$value = $this->object->addRendererToStrategy('a', 'b');
321321
$this->assertNull($value);
322322
}
323+
324+
public function testGetFilePermission()
325+
{
326+
$this->assertNull($this->object->getFilePermission());
327+
}
328+
329+
public function testGetDirPermission()
330+
{
331+
$this->assertNull($this->object->getDirPermission());
332+
}
333+
334+
public function testSetFilePermission()
335+
{
336+
$this->object->setFilePermission('+x');
337+
$this->assertNull($this->object->getFilePermission());
338+
339+
$this->object->setFilePermission(0777);
340+
$this->assertEquals(0777, $this->object->getFilePermission());
341+
}
342+
343+
public function testSetDirPermission()
344+
{
345+
$this->object->setDirPermission('+w');
346+
$this->assertNull($this->object->getDirPermission());
347+
348+
$this->object->setDirPermission(0640);
349+
$this->assertEquals(0640, $this->object->getDirPermission());
350+
}
323351
}

tests/AsseticBundleTest/Service.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ protected function setUp()
5454
],
5555
'options' => [],
5656
],
57+
'deeper_base_css' => [
58+
'assets' => [
59+
'css/global.css',
60+
],
61+
'options' => [
62+
'output' => 'chmod/very/deeper/deeper_base_css.css',
63+
],
64+
],
5765
'base_js' => [
5866
'assets' => [
5967
'js/test.js',
@@ -374,4 +382,46 @@ public function testCacheBusterStrategyWorker()
374382
// cache buster strategy is added to workers list:
375383
$this->assertAttributeEquals([$cacheBusterStrategy], 'workers', $factory);
376384
}
385+
386+
public function testWriteAssetWithSetPermission()
387+
{
388+
$this->configuration->setFilePermission(0777);
389+
$this->configuration->setDirPermission(0770);
390+
391+
$this->object->build();
392+
393+
/** @var \Assetic\Asset\AssetInterface $assets */
394+
$asset = $this->object->getAssetManager()->get('deeper_base_css');
395+
$targetFile = $this->configuration->getWebPath($asset->getTargetPath());
396+
397+
if (is_file($targetFile)) {
398+
unlink($targetFile);
399+
foreach (['chmod/very/deeper', 'chmod/very', 'chmod'] as $name) {
400+
rmdir($this->configuration->getWebPath($name));
401+
}
402+
}
403+
404+
$factory = $this->object->createAssetFactory($this->configuration->getModule('test_application'));
405+
406+
// checking on exists file
407+
$this->assertFileNotExists($targetFile);
408+
$this->object->writeAsset($asset, $factory);
409+
$this->assertFileExists($targetFile);
410+
411+
$filePermisson = function ($path) {
412+
return substr(sprintf('%o', fileperms($path)), -4);
413+
};
414+
415+
// reset cache http://php.net/manual/ru/function.clearstatcache.php
416+
clearstatcache(true, $targetFile);
417+
418+
// checking file permission
419+
$this->assertEquals('0777', $filePermisson($targetFile));
420+
421+
// checking directory permission
422+
foreach (['chmod/very/deeper', 'chmod/very', 'chmod'] as $name) {
423+
$path = $this->configuration->getWebPath($name);
424+
$this->assertEquals('0770', $filePermisson($path));
425+
}
426+
}
377427
}

0 commit comments

Comments
 (0)