PHP Classes

File: src/Loader.php

Recommend this page to a friend!
  Classes of Aleksey   PHP MVC Project   src/Loader.php   Download  
File: src/Loader.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: PHP MVC Project
Framework that implements the MVC design pattern
Author: By
Last change:
Date: 6 years ago
Size: 1,906 bytes
 

Contents

Class file image Download
<?php
/*
 * This file is part of the php-mvc-project <https://github.com/php-mvc-project>
 *
 * Copyright (c) 2018 Aleksey <https://github.com/meet-aleksey>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

spl_autoload_register(function ($name) {
   
$segments = explode('\\', $name);
   
$fileName = array_slice($segments, -1)[0];

   
array_pop($segments);

    if (!empty(
PHPMVC_APP_NAMESPACE) && count($segments) > 0 && $segments[0] == PHPMVC_APP_NAMESPACE) {
       
array_shift($segments);
    }

   
$path = PHPMVC_ROOT_PATH . implode(PHPMVC_DS, $segments) . PHPMVC_DS . $fileName . '.php';

    if (!
is_file($path)) {
       
$segments = array_map('strtolower', $segments);
       
$path = PHPMVC_ROOT_PATH . implode(PHPMVC_DS, $segments) . PHPMVC_DS . $fileName . '.php';
    }

    if (!
is_file($path)) {
        return
false;
    }

    require_once
$path;
});