| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfProjectPermissionsTask extends sfBaseTask |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$current = null, |
|---|
| 23 |
$failed = array(); |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
* @see sfTask |
|---|
| 27 |
*/ |
|---|
| 28 |
protected function configure() |
|---|
| 29 |
{ |
|---|
| 30 |
$this->aliases = array('permissions', 'fix-perms'); |
|---|
| 31 |
$this->namespace = 'project'; |
|---|
| 32 |
$this->name = 'permissions'; |
|---|
| 33 |
$this->briefDescription = 'Fixes symfony directory permissions'; |
|---|
| 34 |
|
|---|
| 35 |
$this->detailedDescription = <<<EOF |
|---|
| 36 |
The [project:permissions|INFO] task fixes directory permissions: |
|---|
| 37 |
|
|---|
| 38 |
[./symfony project:permissions|INFO] |
|---|
| 39 |
EOF; |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
* @see sfTask |
|---|
| 44 |
*/ |
|---|
| 45 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 46 |
{ |
|---|
| 47 |
if (file_exists(sfConfig::get('sf_upload_dir'))) |
|---|
| 48 |
{ |
|---|
| 49 |
$this->chmod(sfConfig::get('sf_upload_dir'), 0777); |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
$this->chmod(sfConfig::get('sf_cache_dir'), 0777); |
|---|
| 53 |
$this->chmod(sfConfig::get('sf_log_dir'), 0777); |
|---|
| 54 |
$this->chmod(sfConfig::get('sf_root_dir').'/symfony', 0777); |
|---|
| 55 |
|
|---|
| 56 |
$dirs = array( |
|---|
| 57 |
sfConfig::get('sf_cache_dir'), |
|---|
| 58 |
sfConfig::get('sf_log_dir'), |
|---|
| 59 |
sfConfig::get('sf_upload_dir'), |
|---|
| 60 |
); |
|---|
| 61 |
|
|---|
| 62 |
$dirFinder = sfFinder::type('dir'); |
|---|
| 63 |
$fileFinder = sfFinder::type('file'); |
|---|
| 64 |
|
|---|
| 65 |
foreach ($dirs as $dir) |
|---|
| 66 |
{ |
|---|
| 67 |
$this->chmod($dirFinder->in($dir), 0777); |
|---|
| 68 |
$this->chmod($fileFinder->in($dir), 0666); |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
if (count($this->failed)) |
|---|
| 73 |
{ |
|---|
| 74 |
$this->logBlock(array_merge( |
|---|
| 75 |
array('Permissions on the following file(s) could not be fixed:', ''), |
|---|
| 76 |
array_map(create_function('$f', 'return \' - \'.sfDebug::shortenFilePath($f);'), $this->failed) |
|---|
| 77 |
), 'ERROR_LARGE'); |
|---|
| 78 |
} |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
* Chmod and capture any failures. |
|---|
| 83 |
* |
|---|
| 84 |
* @param string $file |
|---|
| 85 |
* @param integer $mode |
|---|
| 86 |
* @param integer $umask |
|---|
| 87 |
* |
|---|
| 88 |
* @see sfFilesystem |
|---|
| 89 |
*/ |
|---|
| 90 |
protected function chmod($file, $mode, $umask = 0000) |
|---|
| 91 |
{ |
|---|
| 92 |
if (is_array($file)) |
|---|
| 93 |
{ |
|---|
| 94 |
foreach ($file as $f) |
|---|
| 95 |
{ |
|---|
| 96 |
$this->chmod($f, $mode, $umask); |
|---|
| 97 |
} |
|---|
| 98 |
} |
|---|
| 99 |
else |
|---|
| 100 |
{ |
|---|
| 101 |
set_error_handler(array($this, 'handleError')); |
|---|
| 102 |
|
|---|
| 103 |
$this->current = $file; |
|---|
| 104 |
@$this->getFilesystem()->chmod($file, $mode, $umask); |
|---|
| 105 |
$this->current = null; |
|---|
| 106 |
|
|---|
| 107 |
restore_error_handler(); |
|---|
| 108 |
} |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
* Captures those chmod commands that fail. |
|---|
| 113 |
* |
|---|
| 114 |
* @see http://www.php.net/set_error_handler |
|---|
| 115 |
*/ |
|---|
| 116 |
public function handleError($no, $string, $file, $line, $context) |
|---|
| 117 |
{ |
|---|
| 118 |
$this->failed[] = $this->current; |
|---|
| 119 |
} |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|