helper.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. if (!function_exists('class_basename')) {
  12. /**
  13. * 获取类名(不包含命名空间)
  14. *
  15. * @param string|object $class
  16. * @return string
  17. */
  18. function class_basename($class)
  19. {
  20. $class = is_object($class) ? get_class($class) : $class;
  21. return basename(str_replace('\\', '/', $class));
  22. }
  23. }
  24. if (!function_exists('class_uses_recursive')) {
  25. /**
  26. *获取一个类里所有用到的trait,包括父类的
  27. *
  28. * @param $class
  29. * @return array
  30. */
  31. function class_uses_recursive($class)
  32. {
  33. if (is_object($class)) {
  34. $class = get_class($class);
  35. }
  36. $results = [];
  37. foreach (array_merge([$class => $class], class_parents($class)) as $class) {
  38. $results += trait_uses_recursive($class);
  39. }
  40. return array_unique($results);
  41. }
  42. }
  43. if (!function_exists('trait_uses_recursive')) {
  44. /**
  45. * 获取一个trait里所有引用到的trait
  46. *
  47. * @param string $trait
  48. * @return array
  49. */
  50. function trait_uses_recursive($trait)
  51. {
  52. $traits = class_uses($trait);
  53. foreach ($traits as $trait) {
  54. $traits += trait_uses_recursive($trait);
  55. }
  56. return $traits;
  57. }
  58. }
  59. if (!function_exists('classnames')) {
  60. /**
  61. * css样式名生成器
  62. * classnames("foo", "bar"); // => "foo bar"
  63. * classnames("foo", [ "bar"=> true ]); // => "foo bar"
  64. * classnames([ "foo-bar"=> true ]); // => "foo-bar"
  65. * classnames([ "foo-bar"=> false ]); // => "
  66. * classnames([ "foo" => true ], [ "bar"=> true ]); // => "foo bar"
  67. * classnames([ "foo" => true, "bar"=> true ]); // => "foo bar"
  68. * classnames("foo", [ "bar"=> true, "duck"=> false ], "baz", [ "quux"=> true ]); // => "foo bar baz quux"
  69. * classnames(null, false, "bar", 0, 1, [ "baz"=> null ]); // => "bar 1"
  70. */
  71. function classnames()
  72. {
  73. $args = func_get_args();
  74. $classes = array_map(function ($arg) {
  75. if (is_array($arg)) {
  76. return implode(" ", array_filter(array_map(function ($expression, $class) {
  77. return $expression ? $class : false;
  78. }, $arg, array_keys($arg))));
  79. }
  80. return $arg;
  81. }, $args);
  82. return implode(" ", array_filter($classes));
  83. }
  84. }