Skip to content

Commit

Permalink
New constant, CMB2_DIR and refactor loading process to keep concerns …
Browse files Browse the repository at this point in the history
…separate.
  • Loading branch information
jtsternberg committed Feb 17, 2015
1 parent ada3cca commit 3227991
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
20 changes: 0 additions & 20 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
<?php

/**
* Helper function to provide directory path to CMB
* @since 2.0.0
* @param string $path Path to append
* @return string Directory with optional path appended
*/
function cmb2_dir( $path = '' ) {
static $cmb2_dir = null;
if ( null === $cmb2_dir ) {
$cmb2_dir = trailingslashit( dirname( __FILE__ ) );
}
return $cmb2_dir . $path;
}

/**
* Include helper functions,
* and more importantly, the class/file autoloader
*/
require_once cmb2_dir( 'includes/helper-functions.php' );

/**
* Fires when CMB2 is included/loaded
*
Expand Down
11 changes: 10 additions & 1 deletion includes/helper-functions.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?php

/**
* Helper function to provide directory path to CMB
* @since 2.0.0
* @param string $path Path to append
* @return string Directory with optional path appended
*/
function cmb2_dir( $path = '' ) {
return CMB2_DIR . $path;
}

/**
* Autoloads files with CMB2 classes when needed
* @since 1.0.0
Expand All @@ -15,7 +25,6 @@ function cmb2_autoload_classes( $class_name ) {
include_once( $file );
}
}
spl_autoload_register( 'cmb2_autoload_classes' );

/**
* Get instance of the CMB2_Utils class
Expand Down
12 changes: 12 additions & 0 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,22 @@ private function __construct() {

public function include_cmb() {
if ( ! class_exists( 'CMB2', false ) ) {

if ( ! defined( 'CMB2_VERSION' ) ) {
define( 'CMB2_VERSION', self::VERSION );
}

if ( ! defined( 'CMB2_DIR' ) ) {
define( 'CMB2_DIR', trailingslashit( dirname( __FILE__ ) ) );
}

$this->l10ni18n();

// Include helper functions
require_once 'includes/helper-functions.php';
// Now kick off the class autoloader
spl_autoload_register( 'cmb2_autoload_classes' );
// Kick the whole thing off
require_once 'bootstrap.php';
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/cmb-tests-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public function assertHTMLstringsAreEqual( $expected_string, $string_to_test ) {
return $this->assertEquals( $this->normalize_string( $expected_string ), $this->normalize_string( $string_to_test ) );
}

public function assertIsDefined( $definition ) {
return $this->assertTrue( defined( $definition ), "$definition is not defined." );
}

public function normalize_string( $string ) {
return trim( preg_replace( array(
'/[\t\n\r]/', // Remove tabs and newlines
Expand Down
14 changes: 8 additions & 6 deletions tests/test-cmb-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ public function tearDown() {
parent::tearDown();
}

public function test_cmb2_is_loaded() {
$this->assertTrue( defined( 'CMB2_LOADED' ) );
}

public function test_cmb2_has_version_number() {
$this->assertTrue( defined( 'CMB2_VERSION' ) );
public function test_cmb2_definitions() {
foreach ( array(
'CMB2_LOADED',
'CMB2_VERSION',
'CMB2_DIR',
) as $key => $definition ) {
$this->assertIsDefined( $definition );
}
}

/**
Expand Down

0 comments on commit 3227991

Please sign in to comment.