Util.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace Webman\Console;
  3. use Doctrine\Inflector\InflectorFactory;
  4. class Util
  5. {
  6. public static function nameToNamespace($name)
  7. {
  8. $namespace = ucfirst($name);
  9. $namespace = preg_replace_callback(['/-([a-zA-Z])/', '/(\/[a-zA-Z])/'], function ($matches) {
  10. return strtoupper($matches[1]);
  11. }, $namespace);
  12. return str_replace('/', '\\' ,ucfirst($namespace));
  13. }
  14. public static function classToName($class)
  15. {
  16. $class = lcfirst($class);
  17. return preg_replace_callback(['/([A-Z])/'], function ($matches) {
  18. return '_' . strtolower($matches[1]);
  19. }, $class);
  20. }
  21. public static function nameToClass($class)
  22. {
  23. $class = preg_replace_callback(['/-([a-zA-Z])/', '/_([a-zA-Z])/'], function ($matches) {
  24. return strtoupper($matches[1]);
  25. }, $class);
  26. if (!($pos = strrpos($class, '/'))) {
  27. $class = ucfirst($class);
  28. } else {
  29. $path = substr($class, 0, $pos);
  30. $class = ucfirst(substr($class, $pos + 1));
  31. $class = "$path/$class";
  32. }
  33. return $class;
  34. }
  35. public static function guessPath($base_path, $name, $return_full_path = false)
  36. {
  37. if (!is_dir($base_path)) {
  38. return false;
  39. }
  40. $names = explode('/', trim(strtolower($name), '/'));
  41. $realname = [];
  42. $path = $base_path;
  43. foreach ($names as $name) {
  44. $finded = false;
  45. foreach (scandir($path) ?: [] as $tmp_name) {
  46. if (strtolower($tmp_name) === $name && is_dir("$path/$tmp_name")) {
  47. $path = "$path/$tmp_name";
  48. $realname[] = $tmp_name;
  49. $finded = true;
  50. break;
  51. }
  52. }
  53. if (!$finded) {
  54. return false;
  55. }
  56. }
  57. $realname = implode(DIRECTORY_SEPARATOR, $realname);
  58. return $return_full_path ? get_realpath($base_path . DIRECTORY_SEPARATOR . $realname) : $realname;
  59. }
  60. }