<?php

namespace Signteer;

spl_autoload_register(function($class) {
    $ns = 'Plainwork\\';
    $nslen = strlen($ns);
    if ($ns === substr($class, 0, $nslen)) {
        $shortFile = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, $nslen));
        if (defined('PLAINWORK_PATH')) {
            $dir = PLAINWORK_PATH . DIRECTORY_SEPARATOR . 'src';
        } else {
            $dir = __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Plainwork';
        }
        $file = $dir . DIRECTORY_SEPARATOR . $shortFile . '.php';
        if (file_exists($file)) {
            include_once $file;
        }
        return;
    }

    $ns = __NAMESPACE__ . '\\';
    $nslen = strlen($ns);
    if ($ns === substr($class, 0, $nslen)) {
        $shortFile = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, $nslen));
        $dir = __DIR__ . DIRECTORY_SEPARATOR . 'src';
        $file = $dir . DIRECTORY_SEPARATOR . $shortFile . '.php';
        if (file_exists($file)) {
            include_once $file;
        }
        return;
    }
});
