Application.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Goodquestiondev;
  3. use Illuminate\Support\Str;
  4. /**
  5. * Simple application
  6. */
  7. class Application
  8. {
  9. /**
  10. * The Application version.
  11. *
  12. * @var string
  13. */
  14. const VERSION = '0.1';
  15. /**
  16. * The Application instance.
  17. *
  18. * @var self
  19. */
  20. private static $instance = null;
  21. /**
  22. * The base path for the installation.
  23. *
  24. * @var string
  25. */
  26. protected $basePath;
  27. /**
  28. * The available controllers
  29. *
  30. * @var array
  31. */
  32. public $controllerUris = [];
  33. /**
  34. * Create a new application instance.
  35. *
  36. * @param string|null $basePath
  37. * @return void
  38. */
  39. public function __construct($basePath = null)
  40. {
  41. if ($basePath) {
  42. $this->basePath = rtrim($basePath, '\/');
  43. }
  44. $this->setControllerUris();
  45. }
  46. /**
  47. * Create a new application instance following singleton pattern
  48. *
  49. * @return self
  50. */
  51. public static function getInstance()
  52. {
  53. if (self::$instance == null) {
  54. self::$instance = new Application(__DIR__);
  55. }
  56. return self::$instance;
  57. ;
  58. }
  59. /**
  60. * Get the path to the public directory.
  61. *
  62. * @return string
  63. */
  64. public function publicPath()
  65. {
  66. return $this->basePath.DIRECTORY_SEPARATOR.'public';
  67. }
  68. /**
  69. * Get all available controller URIs
  70. *
  71. * @return array An array of all the available controller URIs
  72. */
  73. public function setControllerUris() : void
  74. {
  75. $files = array_diff(scandir($this->basePath . '/Controllers'), ['..', '.']);
  76. $uris = [];
  77. foreach ($files as $file) {
  78. $withoutExtension = Str::before($file, 'Controller.php');
  79. if (!$withoutExtension) {
  80. continue;
  81. }
  82. $uris[Str::snake($withoutExtension, '.')] = Str::before($file, '.php');
  83. }
  84. $this->controllerUris = $uris;
  85. }
  86. /**
  87. * Get all available controller URIs
  88. *
  89. * @return array An array of all the available controller URIs
  90. */
  91. public function getControllerUris() : array
  92. {
  93. return $this->controllerUris;
  94. }
  95. /**
  96. * Handles the response
  97. *
  98. */
  99. public function respond()
  100. {
  101. return "<html>
  102. <head>
  103. <title>
  104. A Simple HTML Document
  105. </title>
  106. </head>
  107. <body>
  108. <p>This is a very simple HTML document</p>
  109. <p>It only has two paragraphs</p>
  110. </body>
  111. </html>";
  112. }
  113. }