<commit_before><?php

namespace Dat0r;

class AutoloadException extends \Exception {}

class Autoloader
{
    const NS_ROOT = 'Dat0r';

    /**
     * Holds a list of generated domain packages,
     * mapping their namespaces to correspondig base directories.
     *
     * @var array $domainPackages
     */
    private static $domainPackages;

    /**
     * Registers the autoloader to be used by the current process.
     * You may provide the base locations of any generated modules
     * in order to have them autoloaded accordingly.
     *
     * @param array $domainPackages
     */
    static public function register(array $domainPackages = array())
    {
        self::$domainPackages = $domainPackages;

        ini_set('unserialize_callback_func', 'spl_autoload_call');
        spl_autoload_register(array(new self, 'autoload'));
    }

    /**
     * Autoloads a given class.
     *
     * @param string $class
     */
    static public function autoload($class)
    {
        $filePath = FALSE;

        if (0 === strpos($class, self::NS_ROOT))
        {
            $filePath = self::buildCorePath($class);
        }
        else
        {
            foreach (self::$domainPackages as $rootNs => $baseDir)
            {
                if (0 === strpos($class, $rootNs))
                {
                    $filePath = self::buildDomainPath($class, $rootNs, $baseDir);
                    break;
                }
            }
        }
        
        if ($filePath)
        {
            self::tryRequire($filePath);
        }
    }

    /**
     * Builds the class filepath for a given core class.
     *
     * @param string $class
     *
     * @return string
     */
    private static function buildCorePath($class)
    {
        return dirname(__FILE__) . DIRECTORY_SEPARATOR . str_replace(
            array(self::NS_ROOT, '\\'),
            array('', DIRECTORY_SEPARATOR),
            $class
        ) . '.php';
    }

    /**
     * Builds the class filepath for a given domain class.
     *
     * @param string $class
     *
     * @return string
     */
    private static function buildDomainPath($class, $rootNs, $baseDir)
    {
        $baseName = str_replace(
            array($rootNs, '\\'),
            array('', DIRECTORY_SEPARATOR),
            $class
        );
        if (0 === strpos($baseName, '/Base'))
        {
            $baseDir .= DIRECTORY_SEPARATOR . 'base';
        }
        return $baseDir . DIRECTORY_SEPARATOR . $baseName . '.php';
    }

    /**
    * Tries to require a given file.<commit_msg>added a corePackages property to the autoloader and assigned autoload base directories to the core and the tests namespaces.<commit_after><?php

namespace Dat0r;

class AutoloadException extends \Exception {}

class Autoloader
{
    /**
     * Holds a list of domain namespaces mapped to corresponding base dirs for autoloading.
     *
     * @var array $domainPackages
     */
    private static $domainPackages;

    /**
     * Maps Dat0r package namespaces to their corresponding base dir to use for autloading.
     *
     * @var array $corePackages
     */
    private static $corePackages;

    /**
     * Registers the autoloader to be used by the current process.
     * You may provide the base locations of any generated modules
     * in order to have them autoloaded accordingly.
     *
     * @param array $domainPackages
     */
    static public function register(array $domainPackages = array())
    {
        self::$domainPackages = $domainPackages;

        $here = __DIR__;
        self::$corePackages = array(
            'Dat0r\\Core' => $here,
            'Dat0r\\Tests' => dirname(dirname($here)) . '/test/lib/Dat0r'
        );

        ini_set('unserialize_callback_func', 'spl_autoload_call');
        spl_autoload_register(array(new self, 'autoload'));
    }

    /**
     * Autoloads a given class.
     *
     * @param string $class
     */
    static public function autoload($class)
    {
        if (($filePath = (0 === strpos($class, 'Dat0r')) ? self::buildCorePath($class) : self::buildDomainPath($class)))
        {
            self::tryRequire($filePath);
        }
    }

    /**
     * Try to build a path for a Dat0r class.
     *
     * @param string $class
     *
     * @return string
     */
    private static function buildCorePath($class)
    {
        $filePath = NULL;
        foreach (self::$corePackages as $rootNs => $baseDir)
        {
            if (0 === strpos($class, $rootNs))
            {
                $filePath = self::buildPath($class, $rootNs, $baseDir);
                break;
            }
        }
        return $filePath;
    }

    /**
     * Try to build a path for a given domain class.
     *
     * @param string $class
     *
     * @return string
     */
    private static function buildDomainPath($class)
    {
        $filePath = NULL;
        foreach (self::$domainPackages as $rootNs => $baseDir)
        {
            if (0 === strpos($class, $rootNs))
            {
                $filePath = self::buildPath($class, $rootNs, $baseDir);
                break;
            }
        }
        return $filePath;
    }

    /**
     * Builds the class filepath for a given class.
     *
     * @param string $class
     *
     * @return string
     */
    private static function buildPath($class, $rootNs, $baseDir)
    {
        $baseName = str_replace(
            array($rootNs, '\\'),
            array('', DIRECTORY_SEPARATOR),
            $class
        );
        if (0 === strpos($baseName, '/Base'))
        {
            $baseDir .= DIRECTORY_SEPARATOR . 'base';
        }
        return $baseDir . DIRECTORY_SEPARATOR . $baseName . '.php';
    }

    /**
    * Tries to require a given file.
{'fraction_non_alphanumeric': 0.11418624891209747, 'fraction_numerical': 0.00278503046127067, 'mean_word_length': 1.812530592266275, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '84746', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1655, 'n_words': 434}