Skip to content

Commit 973ddec

Browse files
committed
Autoloader improved and simplified
1 parent da64622 commit 973ddec

File tree

9 files changed

+72
-71
lines changed

9 files changed

+72
-71
lines changed

_tools/studio/application/Autoloaders/01_StudioClasses.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
* @author Miroslav Marek <mirek.marek@web-jet.cz>
77
*/
88

9-
namespace JetStudio;
10-
119
use Jet\Autoloader_Loader;
1210
use Jet\SysConf_Path;
1311

@@ -16,31 +14,33 @@
1614
*/
1715
return new class extends Autoloader_Loader
1816
{
17+
public const MAIN_ROOT_NAMESPACE = 'JetStudio\\';
18+
public const WIZARDS_ROOT_NAMESPACE = 'JetStudio\\ModuleWizard\\';
19+
1920
/**
2021
* @return string
2122
*/
2223
public function getAutoloaderName() : string
2324
{
2425
return 'JetStudio/Classes';
2526
}
27+
2628
/**
2729
*
28-
* @param string $root_namespace
29-
* @param string $namespace
3030
* @param string $class_name
3131
*
3232
* @return bool|string
3333
*/
34-
public function getScriptPath( string $root_namespace, string $namespace, string $class_name ): bool|string
34+
public function getScriptPath( string $class_name ): bool|string
3535
{
36-
3736
if(
38-
$namespace != 'JetStudio'
37+
!str_starts_with( $class_name, static::MAIN_ROOT_NAMESPACE ) ||
38+
str_starts_with( $class_name, static::WIZARDS_ROOT_NAMESPACE )
3939
) {
4040
return false;
4141
}
4242

43-
return SysConf_Path::getApplication() . 'Classes/' . $this->classNameToPath($class_name);
43+
return SysConf_Path::getApplication() . 'Classes/' . $this->classNameToPath( substr($class_name, strlen(static::MAIN_ROOT_NAMESPACE)) );
4444

4545
}
4646
};

_tools/studio/application/Autoloaders/02_ModuleWizards.php

+6-11
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
* @author Miroslav Marek <mirek.marek@web-jet.cz>
77
*/
88

9-
namespace JetStudio;
10-
119
use Jet\Autoloader_Loader;
10+
use JetStudio\ModuleWizards;
1211

1312
/**
1413
*
1514
*/
1615
return new class extends Autoloader_Loader
1716
{
17+
public const ROOT_NAMESPACE = 'JetStudio\\ModuleWizard\\';
18+
1819
/**
1920
* @return string
2021
*/
@@ -25,23 +26,17 @@ public function getAutoloaderName() : string
2526

2627
/**
2728
*
28-
* @param string $root_namespace
29-
* @param string $namespace
3029
* @param string $class_name
3130
*
3231
* @return bool|string
3332
*/
34-
public function getScriptPath( string $root_namespace, string $namespace, string $class_name ): bool|string
33+
public function getScriptPath( string $class_name ): bool|string
3534
{
36-
if( !str_starts_with( $namespace, 'JetStudio\ModuleWizard\\' ) ) {
35+
if( !str_starts_with( $class_name, static::ROOT_NAMESPACE ) ) {
3736
return false;
3837
}
3938

40-
$path = substr( $namespace, 23 ) . '\\' . $class_name;
41-
$path = str_replace( '_', DIRECTORY_SEPARATOR, $path );
42-
$path = str_replace( '\\', DIRECTORY_SEPARATOR, $path );
43-
44-
return ModuleWizards::getBasePath() . $path . '.php';
39+
return ModuleWizards::getBasePath() . $this->classNameToPath( substr($class_name, strlen(static::ROOT_NAMESPACE)) );
4540

4641
}
4742
};

_tools/studio/application/Autoloaders/10_ProjectClasses.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* @author Miroslav Marek <mirek.marek@web-jet.cz>
77
*/
88

9-
namespace JetStudio;
10-
119
use Jet\Autoloader_Loader;
10+
use JetStudio\Project;
11+
use JetStudio\ProjectConf_Path;
1212

1313
/**
1414
*
@@ -20,23 +20,23 @@
2020
*/
2121
public function getAutoloaderName() : string
2222
{
23-
return 'application/Classes';
23+
return 'JetStudio/application/Classes';
2424
}
2525

2626
/**
2727
*
28-
* @param string $root_namespace
29-
* @param string $namespace
3028
* @param string $class_name
3129
*
3230
* @return bool|string
3331
*/
34-
public function getScriptPath( string $root_namespace, string $namespace, string $class_name ): bool|string
32+
public function getScriptPath( string $class_name ): bool|string
3533
{
36-
if( $root_namespace != Project::getApplicationNamespace() ) {
34+
$root_namespace = Project::getApplicationNamespace().'\\';
35+
36+
if( !str_starts_with($class_name, $root_namespace ) ) {
3737
return false;
3838
}
39-
40-
return ProjectConf_Path::getApplicationClasses() . $this->classNameToPath( $class_name );
39+
40+
return ProjectConf_Path::getApplicationClasses() . $this->classNameToPath( substr($class_name, strlen($root_namespace)) );
4141
}
4242
};

_tools/studio/application/Autoloaders/11_ProjectModules.php

+18-16
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
* @author Miroslav Marek <mirek.marek@web-jet.cz>
77
*/
88

9-
namespace JetStudio;
10-
119
use Jet\Autoloader_Loader;
1210
use Jet\SysConf_Jet_Modules;
11+
use Jet\Application_Modules;
1312

1413
/**
1514
*
@@ -21,29 +20,32 @@
2120
*/
2221
public function getAutoloaderName() : string
2322
{
24-
return 'application/Modules';
23+
return 'JetStudio/application/Modules';
2524
}
2625
/**
2726
*
28-
* @param string $root_namespace
29-
* @param string $namespace
3027
* @param string $class_name
3128
*
3229
* @return bool|string
3330
*/
34-
public function getScriptPath( string $root_namespace, string $namespace, string $class_name ): bool|string
31+
public function getScriptPath( string $class_name ): bool|string
3532
{
36-
if( $root_namespace != SysConf_Jet_Modules::getModuleRootNamespace() ) {
33+
$modules_namespace = SysConf_Jet_Modules::getModuleRootNamespace().'\\';
34+
35+
if(!str_starts_with($class_name, $modules_namespace)) {
3736
return false;
3837
}
39-
40-
$namespace = substr( $namespace, strlen( $root_namespace ) + 1 );
41-
42-
$module_path = ProjectConf_Path::getApplicationModules() . $namespace . '/';
43-
44-
$module_path = str_replace( '\\', '/', $module_path );
45-
$class_name = str_replace( '_', '/', $class_name );
46-
47-
return $module_path . $class_name . '.php';
38+
39+
$module_and_class_name = substr( $class_name, strlen($modules_namespace) );
40+
41+
$module_name_end = strrpos( $module_and_class_name, '\\' );
42+
43+
$module_name = substr( $module_and_class_name, 0, $module_name_end );
44+
$class_name = substr( $module_and_class_name, $module_name_end+1 );
45+
46+
$module_name = str_replace( '\\', '.', $module_name );
47+
48+
49+
return Application_Modules::getModuleDir( $module_name ) . $this->classNameToPath( $class_name );
4850
}
4951
};

application/Autoloaders/ApplicationClasses.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
return new class extends Autoloader_Loader
1616
{
17+
public const ROOT_NAMESPACE = 'JetApplication\\';
18+
1719
/**
1820
* @return string
1921
*/
@@ -24,20 +26,17 @@ public function getAutoloaderName() : string
2426

2527
/**
2628
*
27-
* @param string $root_namespace
28-
* @param string $namespace
2929
* @param string $class_name
3030
*
3131
* @return bool|string
3232
*/
33-
public function getScriptPath( string $root_namespace, string $namespace, string $class_name ): bool|string
33+
public function getScriptPath( string $class_name ): bool|string
3434
{
35-
36-
if( $root_namespace != 'JetApplication' ) {
35+
if( !str_starts_with($class_name, static::ROOT_NAMESPACE ) ) {
3736
return false;
3837
}
39-
40-
return SysConf_Path::getApplication() . 'Classes/' . $this->classNameToPath( $class_name );
38+
39+
return SysConf_Path::getApplication() . 'Classes/' . $this->classNameToPath( substr($class_name, strlen(static::ROOT_NAMESPACE)) );
4140

4241
}
4342
};

application/Autoloaders/ApplicationModules.php

+14-6
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,27 @@ public function getAutoloaderName() : string
2525

2626
/**
2727
*
28-
* @param string $root_namespace
29-
* @param string $namespace
3028
* @param string $class_name
3129
*
3230
* @return bool|string
3331
*/
34-
public function getScriptPath( string $root_namespace, string $namespace, string $class_name ): bool|string
32+
public function getScriptPath( string $class_name ): bool|string
3533
{
36-
if( $root_namespace != SysConf_Jet_Modules::getModuleRootNamespace() ) {
34+
$modules_namespace = SysConf_Jet_Modules::getModuleRootNamespace().'\\';
35+
36+
if(!str_starts_with($class_name, $modules_namespace)) {
3737
return false;
3838
}
39-
40-
$module_name = str_replace( '\\', '.', substr( $namespace, strlen( $root_namespace ) + 1 ) );
39+
40+
$module_and_class_name = substr( $class_name, strlen($modules_namespace) );
41+
42+
$module_name_end = strrpos( $module_and_class_name, '\\' );
43+
44+
$module_name = substr( $module_and_class_name, 0, $module_name_end );
45+
$class_name = substr( $module_and_class_name, $module_name_end+1 );
46+
47+
$module_name = str_replace( '\\', '.', $module_name );
48+
4149

4250
if( !Application_Modules::moduleIsActivated( $module_name ) ) {
4351
return false;

library/Jet/Autoloader.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,9 @@ public static function initComposerAutoloader() : void
125125
public static function getScriptPath( string $class_name, ?string &$loader_name='' ) : string|bool
126126
{
127127
$path = false;
128-
$root_namespace = strstr( $class_name, '\\', true );
129-
$namespace = substr( $class_name, 0, strrpos( $class_name, '\\' ) );
130-
$_class_name = substr( $class_name, strlen( $namespace ) + 1 );
131-
$loader_name = '';
132128

133129
foreach( static::$loaders as $loader_name => $loader ) {
134-
$path = $loader->getScriptPath( $root_namespace, $namespace, $_class_name );
130+
$path = $loader->getScriptPath( $class_name );
135131
if( $path ) {
136132
break;
137133
}

library/Jet/Autoloader/Loader.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,22 @@ abstract public function getAutoloaderName() : string;
3333

3434
/**
3535
*
36-
* @param string $root_namespace
37-
* @param string $namespace
3836
* @param string $class_name
3937
*
4038
* @return bool|string
4139
*/
42-
abstract public function getScriptPath( string $root_namespace, string $namespace, string $class_name ): bool|string;
40+
abstract public function getScriptPath( string $class_name ): bool|string;
4341

4442
/**
4543
* @param string $class_name
4644
* @return string
4745
*/
4846
public function classNameToPath( string $class_name ) : string
4947
{
50-
return str_replace( '_', DIRECTORY_SEPARATOR, $class_name ) . '.php';
48+
$class_name = ltrim($class_name, '/');
49+
$class_name = str_replace( '_', DIRECTORY_SEPARATOR, $class_name );
50+
$class_name = str_replace( '\\', DIRECTORY_SEPARATOR, $class_name );
51+
52+
return $class_name . '.php';
5153
}
5254
}

library/Jet/JetAutoloader.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,18 @@ public function getAutoloaderName() : string
2424

2525
/**
2626
*
27-
* @param string $root_namespace
28-
* @param string $namespace
2927
* @param string $class_name
3028
*
3129
* @return bool|string
3230
*/
33-
public function getScriptPath( string $root_namespace, string $namespace, string $class_name ): bool|string
31+
public function getScriptPath( string $class_name ): bool|string
3432
{
35-
if( $root_namespace != 'Jet' ) {
33+
34+
if(!str_starts_with($class_name, 'Jet\\')) {
3635
return false;
3736
}
3837

39-
return SysConf_Path::getLibrary() . 'Jet/' . $this->classNameToPath( $class_name );
38+
return SysConf_Path::getLibrary() . $this->classNameToPath( $class_name );
4039

4140
}
4241
};

0 commit comments

Comments
 (0)