Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Simplify autoloading.

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1320

PostPosted: Mon Jun 27, 2016 10:09 am    Post subject: Simplify autoloading. Reply with quote

1. Move smartycompilerexception.php and smartyexception.php up one level. Rename them properly.
2. Populate array of Smarty core classes like this:
Code:
    private static $rootClasses = array(
      'smarty' => 'Smarty.class.php',
      'smartybc' => 'SmartyBC.class.php',
      'smartyexception' => 'SmartyException.php',
      'smartycompilerexception' => 'SmartyCompilerException.php',
      );

3. Then autoloader degrades to a straightforward:
Code:
    /**
     * Handles auto loading of classes.
     *
     * @param string $class A class name.
     */
    public static function autoload($class)
    {
        $_class = strtolower($class);
        if (isset(self::$rootClasses[$_class])) {
            $file = self::$SMARTY_DIR . "/" . self::$rootClasses[$_class];
        }
        elseif (strpos($_class, 'smarty_') === 0) {
            $file = self::$SMARTY_SYSPLUGINS_DIR . "/{$_class}.php";
        }

        if (isset($file) && is_file($file)) {
            return include_once $file;
        }
    }
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7364
Location: Lincoln Nebraska, USA

PostPosted: Tue Jun 28, 2016 1:17 am    Post subject: Reply with quote

submit a PR?
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1320

PostPosted: Tue Jun 28, 2016 1:56 am    Post subject: Reply with quote

If you don't see obvious issues with my suggestion, I'll do that.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1320

PostPosted: Tue Jun 28, 2016 5:52 am    Post subject: Reply with quote

I wanted to avoid moving files, but it seems there's no way around it.
SMARTY_SYSPLUGINS_DIR could be redefined before autoloader is registered.
Which makes the process convoluted again, although in a different place.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7364
Location: Lincoln Nebraska, USA

PostPosted: Tue Jun 28, 2016 12:05 pm    Post subject: Reply with quote

I'm not terribly concerned with the intricacies of the autoloader, as this is not something a developer has to see or directly deal with. What is important is that it works for all intents and purposes. However, if there is a straight-forward way to simplify core code, I'm not adverse to changing it.
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1320

PostPosted: Tue Jun 28, 2016 2:45 pm    Post subject: Reply with quote

The point being, I've hit a situation, where it wanted to throw an exception, but didn't manage to find the class.
After a number of tries to convince the codebase to behave, I ended up rewriting the autoloader from scratch. And then backtracked the changes to the PR.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP