CMS MADE SIMPLE FORGE

CMS Made Simple Core

 

[#9552] Support of Namespace in modules libraries

avatar
Created By: Jean-Christophe Cuvelier (totophe)
Date Submitted: 2013-09-23 12:21

Assigned To:
Resolution: Fixed
State: Closed
Summary:
Support of Namespace in modules libraries
Detailed Description:
This patch add the support of php namespaces into the modules libraries. 

EG: I can now create a class Error with the namespace MyModule and use it inside
my code without harming CMSMS or any other libraries Error class.

Index: lib/autoloader.php
===================================================================
--- lib/autoloader.php	(revision 8989)
+++ lib/autoloader.php	(working copy)
@@ -109,6 +109,9 @@
     {
       foreach( array_keys($list) as $modname )
 	{
+      // Adding basic support of namespaces in module's libraries
+      $parts = explode('\\', $classname);
+      $classname = end($parts);
 	  $fn = $config['root_path']."/modules/$modname/lib/class.$classname.php";
 	  if( file_exists( $fn ) )
 	    {

History

Comments
avatar
Date: 2013-09-23 12:29
Posted By: Jean-Christophe Cuvelier (totophe)

A better version supporting complex namepaths and libraries


Index: lib/autoloader.php
===================================================================
--- lib/autoloader.php	(revision 8989)
+++ lib/autoloader.php	(working copy)
@@ -109,12 +109,24 @@
     {
       foreach( array_keys($list) as $modname )
 	{
+      // Adding basic support of namespaces in module's libraries
+      $parts = explode('\\', $classname);
+      $classname = end($parts);
 	  $fn = $config['root_path']."/modules/$modname/lib/class.$classname.php";
 	  if( file_exists( $fn ) )
 	    {
 	      __cms_load($fn);
 	      return;
 	    }
+        // Adding support of complex namespaces with full path hierarchy
+        $fn = $config['root_path']."/modules/$modname/" .
implode(DIRECTORY_SEPARATOR, $parts) . '.class.php';
+        if( file_exists( $fn ) )
+        {
+            __cms_load($fn);
+            return;
+        }
+
+
 	}
     }
   // module classes

      
avatar
Date: 2013-12-06 05:41
Posted By: Jean-Christophe Cuvelier (totophe)

This is a better implementation for PSR-0 support:

Index: lib/autoloader.php
===================================================================
--- lib/autoloader.php	(revision 9120)
+++ lib/autoloader.php	(working copy)
@@ -120,6 +120,52 @@
   // module classes
 }
 
+function cms_psr0_autoload($className)
+{
+    // IMPLEMENT THE PSR-0 Autoload adapted for CMSMS
+    $config = cmsms()->GetConfig();
+    $className = ltrim($className, '\\');
+    $path = explode('\\', $className);
+    $class = array_pop($path);
+    $vendor = array_shift($path);
+    if(!empty($vendor)) $vendor .= DIRECTORY_SEPARATOR;
+    $namespace = implode(DIRECTORY_SEPARATOR, $path);
+    if(!empty($namespace)) $namespace .= DIRECTORY_SEPARATOR;
+
+    // 1. Lib
+    $lib_path = $config['root_path'] . DIRECTORY_SEPARATOR . $vendor .
$namespace . 'class.' . $class . '.php';
+    if(is_readable($lib_path))
+    {
+        __cms_load($lib_path);
+        return;
+    }
+    // 2. Module
+    if(!empty($vendor))
+    {
+        // Regular
+        $module_path = $config['root_path'] . DIRECTORY_SEPARATOR . 'modules' .
DIRECTORY_SEPARATOR . $vendor . 'lib' . DIRECTORY_SEPARATOR . $namespace .
$class . '.php';
+        if(is_readable($module_path))
+        {
+            __cms_load($module_path);
+            return;
+        }
+        // class.
+        $module_path = $config['root_path'] . DIRECTORY_SEPARATOR . 'modules' .
DIRECTORY_SEPARATOR . $vendor . 'lib' . DIRECTORY_SEPARATOR . $namespace .
'class.' . $class . '.php';
+        if(is_readable($module_path))
+        {
+            __cms_load($module_path);
+            return;
+        }
+        // .class.php
+        $module_path = $config['root_path'] . DIRECTORY_SEPARATOR . 'modules' .
DIRECTORY_SEPARATOR . $vendor . 'lib' . DIRECTORY_SEPARATOR . $namespace .
$class . '.class.php';
+        if(is_readable($module_path))
+        {
+            __cms_load($module_path);
+            return;
+        }
+    }
+}
+
 spl_autoload_register('cms_autoloader');
 
 #

      
avatar
Date: 2013-12-06 05:43
Posted By: Jean-Christophe Cuvelier (totophe)

Correction:

Index: lib/autoloader.php
===================================================================
--- lib/autoloader.php	(revision 9120)
+++ lib/autoloader.php	(working copy)
@@ -120,7 +120,54 @@
   // module classes
 }
 
+function cms_psr0_autoload($className)
+{
+    // IMPLEMENT THE PSR-0 Autoload adapted for CMSMS
+    $config = cmsms()->GetConfig();
+    $className = ltrim($className, '\\');
+    $path = explode('\\', $className);
+    $class = array_pop($path);
+    $vendor = array_shift($path);
+    if(!empty($vendor)) $vendor .= DIRECTORY_SEPARATOR;
+    $namespace = implode(DIRECTORY_SEPARATOR, $path);
+    if(!empty($namespace)) $namespace .= DIRECTORY_SEPARATOR;
+
+    // 1. Lib
+    $lib_path = $config['root_path'] . DIRECTORY_SEPARATOR . $vendor .
$namespace . 'class.' . $class . '.php';
+    if(is_readable($lib_path))
+    {
+        __cms_load($lib_path);
+        return;
+    }
+    // 2. Module
+    if(!empty($vendor))
+    {
+        // Regular
+        $module_path = $config['root_path'] . DIRECTORY_SEPARATOR . 'modules' .
DIRECTORY_SEPARATOR . $vendor . 'lib' . DIRECTORY_SEPARATOR . $namespace .
$class . '.php';
+        if(is_readable($module_path))
+        {
+            __cms_load($module_path);
+            return;
+        }
+        // class.
+        $module_path = $config['root_path'] . DIRECTORY_SEPARATOR . 'modules' .
DIRECTORY_SEPARATOR . $vendor . 'lib' . DIRECTORY_SEPARATOR . $namespace .
'class.' . $class . '.php';
+        if(is_readable($module_path))
+        {
+            __cms_load($module_path);
+            return;
+        }
+        // .class.php
+        $module_path = $config['root_path'] . DIRECTORY_SEPARATOR . 'modules' .
DIRECTORY_SEPARATOR . $vendor . 'lib' . DIRECTORY_SEPARATOR . $namespace .
$class . '.class.php';
+        if(is_readable($module_path))
+        {
+            __cms_load($module_path);
+            return;
+        }
+    }
+}
+
 spl_autoload_register('cms_autoloader');
+spl_autoload_register('cms_psr0_autoload');
 
 #
 # EOF

      
avatar
Date: 2013-12-10 11:55
Posted By: Jean-Christophe Cuvelier (totophe)

-    $lib_path = $config['root_path'] . DIRECTORY_SEPARATOR . $vendor .
$namespace . 'class.' . $class . '.php';
+    $lib_path = $config['root_path'] . DIRECTORY_SEPARATOR . 'lib' .
DIRECTORY_SEPARATOR . $vendor .
$namespace . 'class.' . $class . '.php';
      
avatar
Date: 2016-05-10 14:32
Posted By: Robert Campbell (calguy1000)

similar implementation done for CMSMS 2.2
However, the module must already be loaded.
      
Updates

Updated: 2017-06-10 11:11
state: Open => Closed

Updated: 2016-05-10 14:32
resolution_id: 5 => 7

Updated: 2013-09-23 12:29
resolution_id: => 5