Permalink
Browse files

Sifo now uses default composer autoload.

  • Loading branch information...
obokaman-com committed May 17, 2017
2 parents 84830e1 + 321b3bc commit 603dd7feac712528eac1a90793c4301faf59261c
View
@@ -23,7 +23,8 @@
"geoip/geoip": "^1.17",
"neutron/sphinxsearch-api": "^2.0",
"phpmailer/phpmailer": "^5.2",
- "sifophp/sifo-common-instance": "^2.12",
+ "psr/container": "^1.0",
+ "sifophp/sifo-common-instance": "^2.13",
"smarty/smarty": "3.1.24",
"symfony/yaml": "^3.2",
"tpyo/amazon-s3-php-class": "^0.5",
@@ -37,5 +38,11 @@
"config": {
"optimize-autoloader": true,
"sort-packages": true
+ },
+ "autoload": {
+ "classmap" : ["src/Sifo"]
+ },
+ "autoload-dev": {
+ "classmap" : ["test"]
}
}
View
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<phpunit
+ colors="true"
+ bootstrap="./vendor/autoload.php"
+ convertErrorsToExceptions="true"
+ convertNoticesToExceptions="true"
+ convertWarningsToExceptions="true"
+ beStrictAboutCoversAnnotation="true"
+ beStrictAboutOutputDuringTests="true"
+ beStrictAboutTestSize="true"
+ beStrictAboutTestsThatDoNotTestAnything="false"
+ processIsolation="false"
+ stopOnFailure="false"
+ syntaxCheck="false">
+ <testsuites>
+ <testsuite name="sifo">
+ <directory suffix="Test.php">./test</directory>
+ </testsuite>
+ </testsuites>
+
+ <filter>
+ <whitelist addUncoveredFilesFromWhitelist="true">
+ <directory suffix=".php">./src/Sifo</directory>
+ <exclude>
+ <directory>./vendor</directory>
+ <directory>./test</directory>
+ </exclude>
+ </whitelist>
+ </filter>
+</phpunit>
View
@@ -30,7 +30,6 @@
* @version 0.1
*
* Example of usage in Sifo:
- * $api = $this->getClass( 'APIYoutube' );
* $video_url = 'http://www.youtube.com/watch?v=ybNJb6EuU1Y'; OR $video_url = 'ybNJb6EuU1Y';
* $video_data = $api->getVideoData( $video_url );
*
View
@@ -78,33 +78,6 @@ class Bootstrap
*/
public static $container;
- /**
- * This classes will be loaded in this order and ALWAYS before starting
- * to parse code. This array can be replaced in your libraries.config under
- * the key $config['classes_always_preloaded']
- *
- * @var array
- */
- public static $required_classes = array(
- 'Exceptions',
- 'Filter',
- 'Domains',
- 'Urls',
- 'Router',
- 'Controller'
- );
-
- /**
- * Include the minimum necessary files to run SIFO.
- */
- public static function includeRequiredFiles()
- {
- foreach ( self::$required_classes as $class )
- {
- self::includeFile( $class );
- }
- }
-
/**
* Starts the execution. Root path is passed to avoid recalculation.
*
@@ -120,120 +93,61 @@ public static function execute( $instance_name, $controller_name = null )
self::$root = ROOT_PATH;
self::$application = dirname( __FILE__ );
self::$instance = $instance_name;
+ self::$container = DependencyInjector::getInstance();
- // Include files:
- self::includeRequiredFiles();
-
- self::autoload();
Benchmark::getInstance()->timingStart();
self::dispatch( $controller_name );
Benchmark::getInstance()->timingStop();
}
- /**
- * Registers the autoload used by Sifo.
- *
- * @static
- */
- public static function autoload()
- {
- $autoload = spl_autoload_register(array('\\Sifo\Bootstrap', 'includeFile'));
- self::$container = DependencyInjector::getInstance();
-
- return $autoload;
- }
-
/**
* Invokes a controller with the folder/action form.
*
* @param string $controller The controller in folder/action form.
*
- * @return Controller|void
+ * @return Controller
*/
public static function invokeController( $controller )
{
- $controller_path = explode( '/', $controller );
+ $class = self::convertToControllerClassName( $controller );
- $class = '';
- foreach ( $controller_path as $part )
- {
- $class .= ucfirst( $part );
- }
-
- $class .= 'Controller';
-
- $controller = self::getClass( $class );
+ /** @var Controller $controller */
+ $controller = new $class();
$controller->setContainer(self::$container);
return $controller;
}
- /**
- * Includes (include_once) the file corresponding to the passed passed classname.
- * It does not instantiate any object.
- *
- * This method must be public as it is used in external places, as unit-tests.
- *
- * @param string $classname
- *
- * @throws Exception_500
- * @return string The classname you asked for.
- */
- public static function includeFile( $classname )
- {
- try
- {
- $class_info = Config::getInstance( self::$instance )->getClassInfo( $classname );
- }
- catch ( Exception_Configuration $e )
- {
- return null;
- }
- if (class_exists($class_info['name'], false))
+ private static function convertToControllerClassName( $controller ): string
+ {
+ if(class_exists($controller))
{
- return $class_info['name'];
+ return $controller;
}
- $class_path = ROOT_PATH . DIRECTORY_SEPARATOR . $class_info['path'];
-
- if (!file_exists($class_path))
- {
- throw new Exception_500("Doesn't exist in expected path {$class_info['path']}");
- }
+ $controller_path = explode('/', $controller);
- include_once($class_path);
-
- return $class_info['name'];
- }
+ $class = '';
+ foreach ($controller_path as $part) {
+ $class .= ucfirst($part);
+ }
- /**
- * Returns an instance of the requested class at the lowest level in the hierarchy. The second parameter controls
- * if an instance of the object is returned. If you are getting a class with
- * a private constructor (e.g: singleton) set it to false.
- *
- * @param string $class Class name you want to get
- * @param boolean $call_constructor Return a new object of the class (true), or include the class only (false).
- *
- * @throws Exception_500
- * @return Object|void
- */
- public static function getClass( $class, $call_constructor = true )
- {
- $classname = self::includeFile( $class );
+ $class .= 'Controller';
- if (empty($classname) || !class_exists($classname))
- {
- throw new Exception_500( "Method getClass($class) failed because the class $classname is not declared inside this file (a copy/paste friend?)." );
- }
+ $instance_inheritance = array_reverse(Domains::getInstance()->getInstanceInheritance());
+ foreach ($instance_inheritance as $instance) {
+ $controller_classname = '\\' . ucfirst($instance) . '\\' . $class;
+ if (class_exists($controller_classname)) {
+ $class = $controller_classname;
+ break;
+ }
+ }
- if ($call_constructor)
- {
- return new $classname;
- }
- }
+ return $class;
+ }
/**
* Sets the controller and view properties and executes the controller, sending the output buffer.
@@ -313,7 +227,6 @@ public static function dispatch( $controller = null )
system( 'rm -rf ' . $smarty_compiles_dir );
}
- $ctrl->getClass( 'Cookie' );
if ( FilterGet::getInstance()->getInteger( 'rebuild_all' ) )
{
Cookie::set( 'rebuild_all', 1 );
View
@@ -26,8 +26,6 @@ public static function execute( $instance_name = null, $controller_name = null )
$controller_name = self::$script_controller;
}
- // Register autoloader:
- spl_autoload_register( array( '\\Sifo\Bootstrap', 'includeFile' ) );
self::$container = DependencyInjector::getInstance();
// Set paths:
@@ -104,9 +102,6 @@ public static function get_available_domains()
newrelic_set_appname( ucfirst( CLBootstrap::$instance ) );
}
-// Include required SIFO classes.
-CLBootstrap::includeRequiredFiles();
-
if ( !isset( $argv[1] ) || ( '-h' == $argv[1] ) || ( '--help' == $argv[1] ) )
{
// Dump help info:
View
@@ -157,54 +157,6 @@ public function getConfig( $profile, $group = null )
throw new Exception_Configuration( "The group '$group' for profile '$profile' was never set.", E_USER_ERROR );
}
- /**
- * Given a class name, returns the final class name and path to file.
- *
- * @throws Exception_Configuration When the requested class doesn't exist in the .classes file.
- * @param string $class_type The desired KEY in the configuration file.
- * @return mixed Array with final name class and path.
- */
- public function getClassInfo( $class_type )
- {
- $classes = $this->getConfig( 'classes' );
- $class_type = explode( '\\', $class_type );
- $path = null;
-
- if ( isset( $class_type[1] ) && $class_type[0] == '\\' . $class_type[1] )
- {
- unset( $class_type[1] );
- }
-
- // Append the Namespace on an existing classes.config class.
- if ( isset( $classes[$class_type[0]] ) && !isset( $class_type[1] ) )
- {
- $instances = array_keys( $classes[$class_type[0]] );
- $last_instance = array_pop( $instances );
- array_push( $class_type, $last_instance );
- $path = array_pop( $classes[$class_type[0]] );
- }
- elseif( isset( $class_type[1] ) )
- {
- $class_type = array_reverse( $class_type );
- $path = isset($classes[$class_type[0]][$class_type[1]]) ? $classes[$class_type[0]][$class_type[1]] : null;
- }
-
- if ( isset( $classes[$class_type[0]] ) && !isset( $path ) )
- {
- $path = array_pop( $classes[$class_type[0]] );
- }
-
- if ( !isset( $classes[$class_type[0]] ) )
- {
- // Error handling.
- throw new Exception_Configuration( "The variable '{$class_type[0]}' was not found in the classes file. ", E_USER_ERROR );
- }
-
- // The var is OK, we return the requested array element.
- $class_name = "\\{$class_type[1]}\\$class_type[0]";
- return array( 'name' => $class_name, 'path' => $path );
- }
-
/**
* Instance name.
*
View
@@ -34,13 +34,6 @@
*/
const CACHE_DEFAULT_EXPIRATION = 14400;
- /**
- * List of classes that will be auto-loaded automatically.
- *
- * Format: $include_classes = array( 'Metadata', 'FlashMessages', 'Session', 'Cookie' );
- */
- protected $include_classes = array();
-
/**
* Information useful for debugging.
*
@@ -122,7 +115,6 @@
public function __construct()
{
- $this->includeClasses();
$this->instance = Bootstrap::$instance;
$this->language = Domains::getInstance()->getLanguage();
@@ -213,20 +205,6 @@ protected function getValidatedForm( $submit_button, $form_config, $default_fiel
return $return;
}
- /**
- * Includes all the classes passed in the 'include_classes' attribute.
- */
- protected function includeClasses()
- {
- if ( is_array( $this->include_classes ) && !empty ( $this->include_classes ) )
- {
- foreach ( $this->include_classes as $class )
- {
- $this->getClass( $class, false );
- }
- }
- }
-
public function getUrl( $relative_path, $params = null )
{
$url = Urls::getInstance( $this->instance )->getUrl( $relative_path );
@@ -965,18 +943,6 @@ protected function stopBench( $key, $label )
Debug::subSet( 'benchmarks', $label, Benchmark::getInstance()->timingCurrent( $key ) );
}
- /**
- * Returns an object of the given class.
- *
- * @param string $class_name
- * @param boolean $call_constructor If you want to return a 'new' instance or not. Set to false for singletons.
- * @return Object
- */
- public function getClass( $class_name, $call_constructor = true )
- {
- return Bootstrap::getClass( $class_name, $call_constructor );
- }
-
/**
* Get config from the current instance or a given one.
*
Oops, something went wrong.

0 comments on commit 603dd7f

Please sign in to comment.