Kohana final class


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public static function auto_load($class)
  2. {
  3. if (class_exists($class, FALSE))
  4. return TRUE;
  5.  
  6. if (($suffix = strrpos($class, '_')) > 0)
  7. {
  8. // Find the class suffix
  9. $suffix = substr($class, $suffix + 1);
  10. }
  11. else
  12. {
  13. // No suffix
  14. $suffix = FALSE;
  15. }
  16.  
  17. if ($suffix === 'Core')
  18. {
  19. $type = 'libraries';
  20. $file = substr($class, 0, -5);
  21. }
  22. elseif ($suffix === 'Controller')
  23. {
  24. $type = 'controllers';
  25. // Lowercase filename
  26. $file = strtolower(substr($class, 0, -11));
  27. }
  28. elseif ($suffix === 'Model')
  29. {
  30. $type = 'models';
  31. // Lowercase filename
  32. $file = strtolower(substr($class, 0, -6));
  33. }
  34. elseif ($suffix === 'Driver')
  35. {
  36. $type = 'libraries/drivers';
  37. $file = str_replace('_', '/', substr($class, 0, -7));
  38. }
  39. else
  40. {
  41. // This could be either a library or a helper, but libraries must
  42. // always be capitalized, so we check if the first character is
  43. // uppercase. If it is, we are loading a library, not a helper.
  44. $type = ($class[0] < 'a') ? 'libraries' : 'helpers';
  45. $file = $class;
  46. }
  47.  
  48. if ($filename = self::find_file($type, $file))
  49. {
  50. // Load the class
  51. require $filename;
  52. }
  53. elseif ($type == 'controllers' && $filename = self::find_file($type, Router::$segments[0].'/'.$file))
  54. {
  55. // Load the class from a controllers subfolder
  56. require $filename;
  57. }
  58. else
  59. {
  60. // The class could not be found
  61. return FALSE;
  62. }
  63.  
  64. if ($filename = self::find_file($type, self::$configuration['core']['extension_prefix'].$class))
  65. {
  66. // Load the class extension
  67. require $filename;
  68. }
  69. elseif ($suffix !== 'Core' AND class_exists($class.'_Core', FALSE))
  70. {
  71. // Class extension to be evaluated
  72. $extension = 'class '.$class.' extends '.$class.'_Core { }';
  73.  
  74. // Start class analysis
  75. $core = new ReflectionClass($class.'_Core');
  76.  
  77. if ($core->isAbstract())
  78. {
  79. // Make the extension abstract
  80. $extension = 'abstract '.$extension;
  81. }
  82.  
  83. // Transparent class extensions are handled using eval. This is
  84. // a disgusting hack, but it gets the job done.
  85. eval($extension);
  86. }
  87.  
  88. return TRUE;
  89. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.