<?php
/**
 * The Front Controller for handling every request
 *
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 * @link          https://cakephp.org CakePHP(tm) Project
 * @since         0.2.9
 * @license       MIT License (https://opensource.org/licenses/mit-license.php)
 */
ini_set('default_charset', 'UTF-8');

$uri = explode('/', $_SERVER['REQUEST_URI']);
$target = 'products';
if(count($uri) >= 2){
    $target = $uri[1];
}

// 定数追加
if (!defined('DS')) {
    define('DS', DIRECTORY_SEPARATOR);
}
if (!defined('CAKE_PATH')) {
    define('CAKE_PATH', dirname(__FILE__, 5) . DS . 'cakephp');
}
if (!defined('APP_PATH')) {
    define('APP_PATH', CAKE_PATH . DS . 'app_' . $target);
}

// For built-in server
if (PHP_SAPI === 'cli-server') {
    $_SERVER['PHP_SELF'] = '/' . basename(__FILE__);

    $url = parse_url(urldecode($_SERVER['REQUEST_URI']));
    $file = __DIR__ . $url['path'];
    if (strpos($url['path'], '..') === false && strpos($url['path'], '.') !== false && is_file($file)) {
        return false;
    }
}

require CAKE_PATH . '/vendor/autoload.php';

$targetName = 'AppProducts';
if($target == 'products'){
$targetName = 'AppProducts';
}else if($target == 'service'){
$targetName = 'AppService';
}else{
$targetName = 'AppTrade';
}

use AppProducts\Application;
use AppService\Application as ApplicationService;
use AppTrade\Application as ApplicationTrade;
use Cake\Http\Server;

if($target == 'products'){
	// Bind your application to the server.
	$server = new Server(new Application(APP_PATH . '/config'));
}else if($target == 'service'){
	// Bind your application to the server.
	$server = new Server(new ApplicationService(APP_PATH . '/config'));
}else{
	// Bind your application to the server.
	$server = new Server(new ApplicationTrade(APP_PATH . '/config'));
}


// Run the request/response through the application and emit the response.
$server->emit($server->run());
