PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/core/autoloader.php

https://github.com/wilkerlucio/fishy-framework
PHP | 130 lines | 47 code | 18 blank | 65 comment | 11 complexity | 5d636634b7803d01e8164691f242578a MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2008 Wilker Lucio <wilkerlucio@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. require_once FISHY_SYSTEM_HELPERS_PATH . '/StringHelper.php';
  18. function fishy_autoloader($classname)
  19. {
  20. //check if class already exists
  21. if (class_exists($classname)) {
  22. return true;
  23. }
  24. //check if is a core library
  25. if (Fishy_StringHelper::starts_with($classname, FISHY_SYSTEM_CLASS_PREFIX)) {
  26. $filename = substr($classname, strlen(FISHY_SYSTEM_CLASS_PREFIX));
  27. $path = FISHY_SYSTEM_LIBRARIES_PATH . '/' . $filename . '.php';
  28. if (file_exists($path)) {
  29. require_once $path;
  30. if (class_exists($classname)) {
  31. return true;
  32. }
  33. }
  34. $path = FISHY_SYSTEM_HELPERS_PATH . '/' . $filename . '.php';
  35. if (file_exists($path)) {
  36. require_once $path;
  37. if (class_exists($classname)) {
  38. return true;
  39. }
  40. }
  41. }
  42. //check if the class is a controller
  43. if (Fishy_StringHelper::ends_with($classname, 'Controller')) {
  44. $base_paths = array(FISHY_CONTROLLERS_PATH);
  45. Fishy_ArrayHelper::array_push($base_paths, glob(FISHY_SLICES_PATH . "/*/app/controllers"));
  46. foreach ($base_paths as $path) {
  47. $bits = explode('_', $classname);
  48. while (count($bits) > 1) {
  49. $path .= '/' . array_shift($bits);
  50. }
  51. $path .= '/' . substr(array_shift($bits), 0, -strlen('Controller')) . '.php';
  52. $path = strtolower($path);
  53. if (file_exists($path)) {
  54. require_once $path;
  55. if (class_exists($classname)) {
  56. return true;
  57. }
  58. }
  59. }
  60. }
  61. //try to load user helper
  62. if (Fishy_StringHelper::ends_with($classname, 'Helper')) {
  63. $path = FISHY_HELPERS_PATH;
  64. $bits = explode('_', $classname);
  65. while (count($bits) > 1) {
  66. $path .= '/' . strtolower(array_shift($bits));
  67. }
  68. $path .= '/' . array_shift($bits) . '.php';
  69. if (file_exists($path)) {
  70. require_once $path;
  71. if (class_exists($classname)) {
  72. return true;
  73. }
  74. }
  75. }
  76. //try to load model
  77. $base_paths = array(FISHY_MODELS_PATH);
  78. Fishy_ArrayHelper::array_push($base_paths, glob(FISHY_SLICES_PATH . "/*/app/models"));
  79. foreach ($base_paths as $path) {
  80. $path = $path . '/' . $classname . '.php';
  81. if (file_exists($path)) {
  82. require_once $path;
  83. if (class_exists($classname)) {
  84. return true;
  85. }
  86. }
  87. }
  88. //try user library
  89. $path = FISHY_LIBRARIES_PATH . '/' . $classname . '.php';
  90. if (file_exists($path)) {
  91. require_once $path;
  92. if (class_exists($classname)) {
  93. return true;
  94. }
  95. }
  96. //sorry, i can't found the class :(
  97. return false;
  98. }
  99. //register autoloader
  100. spl_autoload_register('fishy_autoloader');