Util.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. * This file is part of webman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Webman;
  15. use function array_diff;
  16. use function array_map;
  17. use function scandir;
  18. /**
  19. * Class Util
  20. * @package Webman
  21. */
  22. class Util
  23. {
  24. /**
  25. * ScanDir.
  26. * @param string $basePath
  27. * @param bool $withBasePath
  28. * @return array
  29. */
  30. public static function scanDir(string $basePath, bool $withBasePath = true): array
  31. {
  32. if (!is_dir($basePath)) {
  33. return [];
  34. }
  35. $paths = array_diff(scandir($basePath), array('.', '..')) ?: [];
  36. return $withBasePath ? array_map(static function ($path) use ($basePath) {
  37. return $basePath . DIRECTORY_SEPARATOR . $path;
  38. }, $paths) : $paths;
  39. }
  40. }