文件系统助手函数

Testing Is Documentation

tests/Filesystem/HelperTest.php文件系统助手函数 - 图1

Uses

  1. <?php
  2. use Leevel\Filesystem\Helper;

create_directory 创建目录

  1. public function testCreateDirectory(): void
  2. {
  3. $dir = __DIR__.'/createDirectory';
  4. $this->assertDirectoryNotExists($dir);
  5. Helper::createDirectory($dir);
  6. $this->assertDirectoryExists($dir);
  7. Helper::createDirectory($dir);
  8. Helper::createDirectory($dir);
  9. Helper::deleteDirectory($dir);
  10. }

delete_directory 删除目录

  1. public function testDeleteDirectory(): void
  2. {
  3. $dir = __DIR__.'/deleteDirectory/dir';
  4. $this->assertDirectoryNotExists($dir);
  5. Helper::deleteDirectory($dir);
  6. Helper::createDirectory($dir);
  7. $this->assertDirectoryExists($dir);
  8. Helper::deleteDirectory($dir);
  9. $topDir = dirname($dir);
  10. $this->assertDirectoryExists($topDir);
  11. Helper::deleteDirectory($topDir);
  12. $this->assertDirectoryNotExists($topDir);
  13. }

traverse_directory 遍历目录

  1. public function testTraverseDirectory(): void
  2. {
  3. $sourcePath = __DIR__.'/traverseDirectory';
  4. $sourceSubPath = __DIR__.'/traverseDirectory/dir';
  5. $this->assertDirectoryNotExists($sourceSubPath);
  6. Helper::createDirectory($sourceSubPath);
  7. file_put_contents($testFile = $sourceSubPath.'/hello.txt', 'foo');
  8. $this->assertTrue(is_file($testFile));
  9. $this->assertSame('foo', file_get_contents($testFile));
  10. $filesAndDirs = [];
  11. $filesAndDirs2 = [];
  12. Helper::traverseDirectory($sourcePath, true, function ($item) use (&$filesAndDirs) {
  13. $filesAndDirs[] = $item->getFileName();
  14. });
  15. Helper::traverseDirectory($sourcePath, true, function ($item) use (&$filesAndDirs2) {
  16. $filesAndDirs2[] = $item->getFileName();
  17. }, ['hello.txt']);
  18. $this->assertSame(['dir', 'hello.txt'], $filesAndDirs);
  19. $this->assertSame(['dir'], $filesAndDirs2);
  20. Helper::deleteDirectory($sourcePath);
  21. }

tidy_path 整理目录斜线风格

  1. public function testTidyPath(): void
  2. {
  3. $sourcePath = '/home\goods/name/';
  4. $this->assertSame('/home/goods/name', Helper::tidyPath($sourcePath));
  5. $this->assertSame('\home\goods\name', Helper::tidyPath($sourcePath, false));
  6. }

is_absolute_path 判断是否为绝对路径

  1. public function testIsAbsolutePath(): void
  2. {
  3. $this->assertTrue(Helper::isAbsolutePath('c://'));
  4. $this->assertTrue(Helper::isAbsolutePath('/path/hello'));
  5. $this->assertFalse(Helper::isAbsolutePath('hello'));
  6. }

distributed 根据 ID 获取打散目录

  1. public function testDistributed(): void
  2. {
  3. $this->assertSame(['000/00/00/', '01'], Helper::distributed(1));
  4. $this->assertSame(['090/00/00/', '00'], Helper::distributed(90000000));
  5. }

create_file 创建文件

  1. public function testCreateFile(): void
  2. {
  3. $sourcePath = __DIR__.'/createFile';
  4. $file = $sourcePath.'/hello.txt';
  5. $this->assertDirectoryNotExists($sourcePath);
  6. Helper::createDirectory($sourcePath);
  7. $this->assertFalse(is_file($file));
  8. Helper::createFile($file);
  9. $this->assertTrue(is_file($file));
  10. Helper::deleteDirectory($sourcePath);
  11. }

get_extension 获取上传文件扩展名

  1. public function testGetExtension(): void
  2. {
  3. $file = __DIR__.'/HelperTest.pHp';
  4. $this->assertSame('pHp', Helper::getExtension($file));
  5. $this->assertSame('PHP', Helper::getExtension($file, 1));
  6. $this->assertSame('php', Helper::getExtension($file, 2));
  7. }