Caldera_Forms_Autoloader

Autoloader for Caldera Forms and Caldera Forms add-ons.

Defined (1)

The class is defined in the following location(s).

/classes/autoloader.php  
  1. class Caldera_Forms_Autoloader { 
  2.  
  3. /** 
  4. * Prefixes for our psuedo-namespaces 
  5. * 'prefix' => 'path' 
  6. * @since 1.3.5.3 
  7. * @var array 
  8. */ 
  9. protected static $roots = array(); 
  10.  
  11. /** 
  12. * Add a route path and prefix 
  13. * @since 1.3.5.3 
  14. * @param string $prefix Class prefix -- Must start with "Caldera_Forms" or "CF_" use "CF_" for add-ons 
  15. * @param string $dir Full path to directory 
  16. */ 
  17. public static function add_root( $prefix, $dir ) { 
  18. self::$roots[ $prefix ] = $dir; 
  19.  
  20.  
  21. /** 
  22. * Handles autoloading of Caldera Forms and Caldera Forms add-on classes. 
  23. * @since 1.3.5.3 
  24. * @param string $class 
  25. */ 
  26. public static function autoload( $class ) { 
  27. if ( 0 === strpos( $class, 'Caldera_Forms' ) || 0 === strpos( $class, 'CF_' ) ) { 
  28.  
  29.  
  30. $root = self::find_root( $class ); 
  31. if ( ! $root ) { 
  32. return; 
  33.  
  34. $dir = self::get_dir( $root ); 
  35.  
  36. if ( 'Caldera_Forms' == $class ) { 
  37. $file = $dir . 'core.php'; 
  38. } elseif ( 'Caldera_Forms_Fields' === $class ) { 
  39. $file = CFCORE_PATH . 'classes/fields.php'; 
  40. } elseif ( 'Caldera_Forms_Magic' === $class ) { 
  41. $file = CFCORE_PATH . 'classes/magic.php'; 
  42. }elseif ( 'Caldera_Form_Grid' == $class ) { 
  43. $file = $dir . 'caldera-grid.php'; 
  44. } elseif( 'Caldera_Forms_Entry' == $class ) { 
  45. $file = CFCORE_PATH . 'classes/entry.php'; 
  46. } elseif ( 'Caldera_Forms_Save_Final' == $class ) { 
  47. $file = CFCORE_PATH . 'classes/save.php'; 
  48. } elseif( 'Caldera_Forms_Admin' == $class ) { 
  49. $file = CFCORE_PATH . 'classes/admin.php'; 
  50. }else { 
  51. $file = $dir . self::get_base( $class, $root ); 
  52.  
  53.  
  54. if ( is_file( $file ) ) { 
  55. require_once $file; 
  56.  
  57.  
  58.  
  59. /** 
  60. * Get the root prefix for a class 
  61. * @since 1.3.5.3 
  62. * @param string $class Class name 
  63. * @return string|void 
  64. */ 
  65. protected static function find_root( $class ) { 
  66. foreach( self::$roots as $root => $dir ) { 
  67. if( 0 === strpos( $class, $root ) ) { 
  68. return $root; 
  69.  
  70.  
  71.  
  72. /** 
  73. * Get the directory for a prefix 
  74. * @since 1.3.5.3 
  75. * @param string $root Prefix root 
  76. * @return string|void 
  77. */ 
  78. protected static function get_dir( $root ) { 
  79. if( 'Caldera_Forms_Fields') 
  80. if( array_key_exists( $root, self::$roots ) ) { 
  81. return trailingslashit( self::$roots[ $root ] ); 
  82.  
  83. /** 
  84. * Get file name for class 
  85. * @param string $class Class name 
  86. * @param string $root Prefix root 
  87. * @return string 
  88. */ 
  89. protected static function get_base( $class, $root ) { 
  90. return strtolower( str_replace( $root . '_', '', $class ) ) . '.php'; 
  91.  
  92. /** 
  93. * Registers Caldera_Forms_Autoloader as an SPL autoloader. 
  94. * @since 1.3.5.3 
  95.   
  96. */ 
  97. public static function register( ) { 
  98. if ( version_compare( phpversion(), '5.3.0', '>=' ) ) { 
  99. spl_autoload_register( array( new self(), 'autoload' ), true, false ); 
  100. } else { 
  101. spl_autoload_register( array( new self(), 'autoload' ) ); 
  102.  
  103.