[Filesystem] chown and chgrp should also accept int as owner and group
This commit is contained in:
parent
940bba0860
commit
eba5a0c390
@ -207,10 +207,11 @@ class Filesystem
|
|||||||
* Change the owner of an array of files or directories.
|
* Change the owner of an array of files or directories.
|
||||||
*
|
*
|
||||||
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change owner
|
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change owner
|
||||||
|
* @param string|int $user A user name or number
|
||||||
*
|
*
|
||||||
* @throws IOException When the change fails
|
* @throws IOException When the change fails
|
||||||
*/
|
*/
|
||||||
public function chown($files, string $user, bool $recursive = false)
|
public function chown($files, $user, bool $recursive = false)
|
||||||
{
|
{
|
||||||
foreach ($this->toIterable($files) as $file) {
|
foreach ($this->toIterable($files) as $file) {
|
||||||
if ($recursive && is_dir($file) && !is_link($file)) {
|
if ($recursive && is_dir($file) && !is_link($file)) {
|
||||||
@ -232,10 +233,11 @@ class Filesystem
|
|||||||
* Change the group of an array of files or directories.
|
* Change the group of an array of files or directories.
|
||||||
*
|
*
|
||||||
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change group
|
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change group
|
||||||
|
* @param string|int $group A group name or number
|
||||||
*
|
*
|
||||||
* @throws IOException When the change fails
|
* @throws IOException When the change fails
|
||||||
*/
|
*/
|
||||||
public function chgrp($files, string $group, bool $recursive = false)
|
public function chgrp($files, $group, bool $recursive = false)
|
||||||
{
|
{
|
||||||
foreach ($this->toIterable($files) as $file) {
|
foreach ($this->toIterable($files) as $file) {
|
||||||
if ($recursive && is_dir($file) && !is_link($file)) {
|
if ($recursive && is_dir($file) && !is_link($file)) {
|
||||||
|
@ -521,7 +521,7 @@ class FilesystemTest extends FilesystemTestCase
|
|||||||
$this->assertFilePermissions(753, $subdirectory);
|
$this->assertFilePermissions(753, $subdirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testChown()
|
public function testChownByName()
|
||||||
{
|
{
|
||||||
$this->markAsSkippedIfPosixIsMissing();
|
$this->markAsSkippedIfPosixIsMissing();
|
||||||
|
|
||||||
@ -534,7 +534,20 @@ class FilesystemTest extends FilesystemTestCase
|
|||||||
$this->assertSame($owner, $this->getFileOwner($dir));
|
$this->assertSame($owner, $this->getFileOwner($dir));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testChownRecursive()
|
public function testChownById()
|
||||||
|
{
|
||||||
|
$this->markAsSkippedIfPosixIsMissing();
|
||||||
|
|
||||||
|
$dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
|
||||||
|
mkdir($dir);
|
||||||
|
|
||||||
|
$ownerId = $this->getFileOwnerId($dir);
|
||||||
|
$this->filesystem->chown($dir, $ownerId);
|
||||||
|
|
||||||
|
$this->assertSame($ownerId, $this->getFileOwnerId($dir));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testChownRecursiveByName()
|
||||||
{
|
{
|
||||||
$this->markAsSkippedIfPosixIsMissing();
|
$this->markAsSkippedIfPosixIsMissing();
|
||||||
|
|
||||||
@ -549,6 +562,21 @@ class FilesystemTest extends FilesystemTestCase
|
|||||||
$this->assertSame($owner, $this->getFileOwner($file));
|
$this->assertSame($owner, $this->getFileOwner($file));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testChownRecursiveById()
|
||||||
|
{
|
||||||
|
$this->markAsSkippedIfPosixIsMissing();
|
||||||
|
|
||||||
|
$dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
|
||||||
|
mkdir($dir);
|
||||||
|
$file = $dir.\DIRECTORY_SEPARATOR.'file';
|
||||||
|
touch($file);
|
||||||
|
|
||||||
|
$ownerId = $this->getFileOwnerId($dir);
|
||||||
|
$this->filesystem->chown($dir, $ownerId, true);
|
||||||
|
|
||||||
|
$this->assertSame($ownerId, $this->getFileOwnerId($file));
|
||||||
|
}
|
||||||
|
|
||||||
public function testChownSymlink()
|
public function testChownSymlink()
|
||||||
{
|
{
|
||||||
$this->markAsSkippedIfSymlinkIsMissing();
|
$this->markAsSkippedIfSymlinkIsMissing();
|
||||||
@ -624,7 +652,7 @@ class FilesystemTest extends FilesystemTestCase
|
|||||||
$this->filesystem->chown($dir, 'user'.time().mt_rand(1000, 9999));
|
$this->filesystem->chown($dir, 'user'.time().mt_rand(1000, 9999));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testChgrp()
|
public function testChgrpByName()
|
||||||
{
|
{
|
||||||
$this->markAsSkippedIfPosixIsMissing();
|
$this->markAsSkippedIfPosixIsMissing();
|
||||||
|
|
||||||
@ -637,6 +665,19 @@ class FilesystemTest extends FilesystemTestCase
|
|||||||
$this->assertSame($group, $this->getFileGroup($dir));
|
$this->assertSame($group, $this->getFileGroup($dir));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testChgrpById()
|
||||||
|
{
|
||||||
|
$this->markAsSkippedIfPosixIsMissing();
|
||||||
|
|
||||||
|
$dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
|
||||||
|
mkdir($dir);
|
||||||
|
|
||||||
|
$groupId = $this->getFileGroupId($dir);
|
||||||
|
$this->filesystem->chgrp($dir, $groupId);
|
||||||
|
|
||||||
|
$this->assertSame($groupId, $this->getFileGroupId($dir));
|
||||||
|
}
|
||||||
|
|
||||||
public function testChgrpRecursive()
|
public function testChgrpRecursive()
|
||||||
{
|
{
|
||||||
$this->markAsSkippedIfPosixIsMissing();
|
$this->markAsSkippedIfPosixIsMissing();
|
||||||
@ -652,7 +693,7 @@ class FilesystemTest extends FilesystemTestCase
|
|||||||
$this->assertSame($group, $this->getFileGroup($file));
|
$this->assertSame($group, $this->getFileGroup($file));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testChgrpSymlink()
|
public function testChgrpSymlinkByName()
|
||||||
{
|
{
|
||||||
$this->markAsSkippedIfSymlinkIsMissing();
|
$this->markAsSkippedIfSymlinkIsMissing();
|
||||||
|
|
||||||
@ -669,6 +710,23 @@ class FilesystemTest extends FilesystemTestCase
|
|||||||
$this->assertSame($group, $this->getFileGroup($link));
|
$this->assertSame($group, $this->getFileGroup($link));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testChgrpSymlinkById()
|
||||||
|
{
|
||||||
|
$this->markAsSkippedIfSymlinkIsMissing();
|
||||||
|
|
||||||
|
$file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
|
||||||
|
$link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
|
||||||
|
|
||||||
|
touch($file);
|
||||||
|
|
||||||
|
$this->filesystem->symlink($file, $link);
|
||||||
|
|
||||||
|
$groupId = $this->getFileGroupId($link);
|
||||||
|
$this->filesystem->chgrp($link, $groupId);
|
||||||
|
|
||||||
|
$this->assertSame($groupId, $this->getFileGroupId($link));
|
||||||
|
}
|
||||||
|
|
||||||
public function testChgrpLink()
|
public function testChgrpLink()
|
||||||
{
|
{
|
||||||
$this->markAsSkippedIfLinkIsMissing();
|
$this->markAsSkippedIfLinkIsMissing();
|
||||||
|
@ -105,21 +105,36 @@ class FilesystemTestCase extends TestCase
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getFileOwner($filepath)
|
protected function getFileOwnerId($filepath)
|
||||||
{
|
{
|
||||||
$this->markAsSkippedIfPosixIsMissing();
|
$this->markAsSkippedIfPosixIsMissing();
|
||||||
|
|
||||||
$infos = stat($filepath);
|
$infos = stat($filepath);
|
||||||
|
|
||||||
return ($datas = posix_getpwuid($infos['uid'])) ? $datas['name'] : null;
|
return $infos['uid'];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getFileOwner($filepath)
|
||||||
|
{
|
||||||
|
$this->markAsSkippedIfPosixIsMissing();
|
||||||
|
|
||||||
|
return ($datas = posix_getpwuid($this->getFileOwnerId($filepath))) ? $datas['name'] : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getFileGroupId($filepath)
|
||||||
|
{
|
||||||
|
$this->markAsSkippedIfPosixIsMissing();
|
||||||
|
|
||||||
|
$infos = stat($filepath);
|
||||||
|
|
||||||
|
return $infos['gid'];
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getFileGroup($filepath)
|
protected function getFileGroup($filepath)
|
||||||
{
|
{
|
||||||
$this->markAsSkippedIfPosixIsMissing();
|
$this->markAsSkippedIfPosixIsMissing();
|
||||||
|
|
||||||
$infos = stat($filepath);
|
if ($datas = posix_getgrgid($this->getFileGroupId($filepath))) {
|
||||||
if ($datas = posix_getgrgid($infos['gid'])) {
|
|
||||||
return $datas['name'];
|
return $datas['name'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user