Any way to avoid using the phar file? #38

Closed
AutoDMC opened this Issue Apr 9, 2012 · 2 comments

Comments

Projects
None yet
3 participants

AutoDMC commented Apr 9, 2012

I'm trying to debug my application's interaction with the Morph library, but having to rebuild the phar file just to drop a line of logging into the library seems a bit overkill.

I'm not experienced with phars, and I don't see any information about just using the library directly. Is it just not possible to do so?

Owner

a-musing-moose commented Apr 17, 2012

Whilst Morph has been designed to work as a phar file it is possible to use it in an unpackaged manner, it is just a bit of work. Take a look at bootstrap.php ( https://github.com/a-musing-moose/morph/blob/master/src/bootstrap.php ). This is the file than the phar file runs when it is loaded. Basically all it does is add a new autoloader which knows how to find the classes when they are referenced. Not sure if you are familiar with autoloaders but it would be pretty easy to modify this file (or create a new one) to map not to the phar file but to just some folder on your php include path. That way you can use the library unpackaged.

Hope that all makes sense?
Regards,
Jon

Contributor

MiniCodeMonkey commented Apr 18, 2012

I don't wanna say that this is the best solution, but i basically made a copy of the bootstrap.php script that works as a standalone autoloader:

<?php
class MorphAutoloader
{

    /**
     * A static array of classes
     *
     * @var array
     */
    private static $classes = array(
        'morph\\Collection'                  => 'phar://Morph/Collection.php',
        'morph\\Enum'                        => 'phar://Morph/Enum.php',
        'morph\\Utils'                       => 'phar://Morph/Utils.php',
        'morph\\Iterator'                    => 'phar://Morph/Iterator.php',
        'morph\\compare\\NumericProperty'    => 'phar://Morph/compare/NumericProperty.php',
        'morph\\compare\\Property'           => 'phar://Morph/compare/Property.php',
        'morph\\PropertySet'                 => 'phar://Morph/PropertySet.php',
        'morph\\Query'                       => 'phar://Morph/Query.php',
        'morph\\IQuery'                      => 'phar://Morph/IQuery.php',
        'morph\\Object'                      => 'phar://Morph/Object.php',
        'morph\\ICompare'                    => 'phar://Morph/ICompare.php',
        'morph\\Storage'                     => 'phar://Morph/Storage.php',
        'morph\\property\\HasMany'           => 'phar://Morph/property/HasMany.php',
        'morph\\property\\Date'              => 'phar://Morph/property/Date.php',
        'morph\\property\\ComposeMany'       => 'phar://Morph/property/ComposeMany.php',
        'morph\\property\\HasOne'            => 'phar://Morph/property/HasOne.php',
        'morph\\property\\Enum'              => 'phar://Morph/property/Enum.php',
        'morph\\property\\Integer'           => 'phar://Morph/property/Integer.php',
        'morph\\property\\File'              => 'phar://Morph/property/File.php',
        'morph\\property\\Float'             => 'phar://Morph/property/Float.php',
        'morph\\property\\ComposeOne'        => 'phar://Morph/property/ComposeOne.php',
        'morph\\property\\String'            => 'phar://Morph/property/String.php',
        'morph\\property\\Generic'           => 'phar://Morph/property/Generic.php',
        'morph\\property\\Boolean'           => 'phar://Morph/property/Boolean.php',
        'morph\\property\\BinaryData'        => 'phar://Morph/property/BinaryData.php',
        'morph\\property\\Integer32'         => 'phar://Morph/property/Integer32.php',
        'morph\\property\\Integer64'         => 'phar://Morph/property/Integer64.php',
        'morph\\property\\Regex'             => 'phar://Morph/property/Regex.php',
        'morph\\property\\Complex'           => 'phar://Morph/property/Complex.php',
        'morph\\property\\StatefulCollection'=> 'phar://Morph/property/StatefulCollection.php',
        'morph\\query\\Property'             => 'phar://Morph/query/Property.php',
        'morph\\format\\Collection'          => 'phar://Morph/format/Collection.php',
        'morph\\exception\\ObjectNotFound'   => 'phar://Morph/exception/ObjectNotFound.php',
    );

    /**
     * class loader
     *
     * @param string $className
     * @return boolean
     */
    public static function load($className)
    {
        $isLoaded = false;
        if (isset(self::$classes[$className])) {
            include dirname(__FILE__) . '/' . str_replace('\\', '/', $className) . '.php';
            $isLoaded = true;
        }
        return $isLoaded;
    }

}

//register the autoloader
spl_autoload_register(array('MorphAutoloader', 'load'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment