Diagnoser.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /*
  3. * This file is part of the Fidry CPUCounter Config package.
  4. *
  5. * (c) Théo FIDRY <theo.fidry@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Fidry\CpuCoreCounter;
  12. use Fidry\CpuCoreCounter\Finder\CpuCoreFinder;
  13. use function array_map;
  14. use function explode;
  15. use function implode;
  16. use function max;
  17. use function str_repeat;
  18. use const PHP_EOL;
  19. /**
  20. * Utility to debug.
  21. *
  22. * @private
  23. */
  24. final class Diagnoser
  25. {
  26. /**
  27. * Provides an aggregated diagnosis based on each finders diagnosis.
  28. *
  29. * @param list<CpuCoreFinder> $finders
  30. */
  31. public static function diagnose(array $finders): string
  32. {
  33. $diagnoses = array_map(
  34. static function (CpuCoreFinder $finder): string {
  35. return self::diagnoseFinder($finder);
  36. },
  37. $finders
  38. );
  39. return implode(PHP_EOL, $diagnoses);
  40. }
  41. /**
  42. * Executes each finders.
  43. *
  44. * @param list<CpuCoreFinder> $finders
  45. */
  46. public static function execute(array $finders): string
  47. {
  48. $diagnoses = array_map(
  49. static function (CpuCoreFinder $finder): string {
  50. $coresCount = $finder->find();
  51. return implode(
  52. '',
  53. [
  54. $finder->toString(),
  55. ': ',
  56. null === $coresCount ? 'NULL' : $coresCount,
  57. ]
  58. );
  59. },
  60. $finders
  61. );
  62. return implode(PHP_EOL, $diagnoses);
  63. }
  64. private static function diagnoseFinder(CpuCoreFinder $finder): string
  65. {
  66. $diagnosis = $finder->diagnose();
  67. $maxLineLength = max(
  68. array_map(
  69. 'strlen',
  70. explode(PHP_EOL, $diagnosis)
  71. )
  72. );
  73. $separator = str_repeat('-', $maxLineLength);
  74. return implode(
  75. '',
  76. [
  77. $finder->toString().':'.PHP_EOL,
  78. $separator.PHP_EOL,
  79. $diagnosis.PHP_EOL,
  80. $separator.PHP_EOL,
  81. ]
  82. );
  83. }
  84. private function __construct()
  85. {
  86. }
  87. }