Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

JLoader : Files located at the root path, where the prefix is registered... #1445

Closed
wants to merge 1 commit into from

2 participants

Florian Voutzinos Rouven Weßling
Florian Voutzinos

No description provided.

Rouven Weßling
Collaborator

I can see how this is useful but I'm afraid of the performance implications. In some testing I did we spend up to 20% of the execution time in the autoloader. If we add more it certainly won't help.

I let other s judge whether it's worth the trade off, but frankly I don't see the big deal with following our folder strcuture. We could put the factory into a factory folder and it would be autoloadable too.

Florian Voutzinos

I added the code at the end, I don't think it changes a lot the current execution time : it adds one variable allocation, and two when there is only one part in the class name.

The if is never executed for platform classes, because there is only one lookup path.

Rouven Weßling
Collaborator

Well it is for all legacy classes. For example in the Joomla CMS we have 3 autoloaders for J prefix so this code would be executed twice in the worst case.

Another thing that just occurred to me is that with this patch we don't have a 1:1 mapping of class names and file paths anymore. Again not a big deal but something to consider (and document).

Edith: The thing is also not the variable but the file_exists() call.

Florian Voutzinos

Yes, in the case of the CMS it can add two file_exists calls in some cases when there is only one part.

What I was thinking also regarding my namespace loader pull request is eventually to have more than one autoloader.

In this case, JLoader would stay like that to auto load the core, and we can have a JLoaderNamespace, and eventually an other Loader with this feature.

It can allow people to use the auto loader they want for their application/components without making the core execution slower, and avoid to register more methods by default to the spl autoloader stack.

Florian Voutzinos florianv closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 10, 2012
  1. Florian Voutzinos

    JLoader : Files located at the root path, where the prefix is registe…

    florianv authored
    …red, cannot be autoloaded.
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 2 deletions.
  1. +24 −2 libraries/loader.php
26 libraries/loader.php
View
@@ -318,8 +318,17 @@ private static function _load($class, $lookup)
// Split the class name into parts separated by camelCase.
$parts = preg_split('/(?<=[a-z0-9])(?=[A-Z])/x', $class);
- // If there is only one part we want to duplicate that part for generating the path.
- $parts = (count($parts) === 1) ? array($parts[0], $parts[0]) : $parts;
+ $fileName = false;
+
+ // If there is only one part.
+ if (count($parts) == 1)
+ {
+ // Keep the possible file name.
+ $fileName = $parts[0];
+
+ // Duplicate that part for generating the path.
+ $parts = array($parts[0], $parts[0]);
+ }
foreach ($lookup as $base)
{
@@ -331,6 +340,19 @@ private static function _load($class, $lookup)
{
return include $path;
}
+
+ // If there is only one part.
+ if ($fileName)
+ {
+ // Try to include the class that might be located in the root folder.
+ $path = $base . '/' . strtolower($fileName) . '.php';
+
+ // Load the file if it exists.
+ if (file_exists($path))
+ {
+ return include $path;
+ }
+ }
}
}
}
Something went wrong with that request. Please try again.