Exemplo n.º 1
0
// Debug::enable();
ErrorHandler::register();
ExceptionHandler::register();
$config = (include __DIR__ . '/config.php');
$config['host'] = 'http://' . $_SERVER['HTTP_HOST'];
$connection = DriverManager::getConnection(array('dbname' => $config['database_name'], 'user' => $config['database_user'], 'password' => $config['database_password'], 'host' => $config['database_host'], 'driver' => $config['database_driver'], 'charset' => 'utf8'));
$serviceKernel = ServiceKernel::create($config['environment'], true);
$serviceKernel->setParameterBag(new ParameterBag($config));
$serviceKernel->setConnection($connection);
// $serviceKernel->getConnection()->exec('SET NAMES UTF8');
include __DIR__ . '/src/functions.php';
$app = new Silex\Application();
$app->register(new Silex\Provider\ServiceControllerServiceProvider());
$app['debug'] = true;
$app->view(function (array $result, Request $request) use($app) {
    return new JsonResponse($result);
});
include __DIR__ . '/config/container.php';
$app->before(function (Request $request) use($app) {
    $whiteLists = (include __DIR__ . '/whiteList.php');
    $whiteLists = $request->getMethod() == 'GET' ? $whiteLists['get'] : $whiteLists['post'];
    $inWhiteList = 0;
    foreach ($whiteLists as $whiteList) {
        $path = $request->getPathInfo();
        if (preg_match($whiteList, $request->getPathInfo())) {
            $inWhiteList = 1;
            break;
        }
    }
    $token = $request->headers->get('Auth-Token', '');
    if (!$inWhiteList && empty($token)) {
Exemplo n.º 2
0
$paramaters['host'] = 'http://' . $_SERVER['HTTP_HOST'];
$connection = DriverManager::getConnection(array('wrapperClass' => 'Topxia\\Service\\Common\\Connection', 'dbname' => $paramaters['database_name'], 'user' => $paramaters['database_user'], 'password' => $paramaters['database_password'], 'host' => $paramaters['database_host'], 'driver' => $paramaters['database_driver'], 'charset' => 'utf8'));
$serviceKernel = ServiceKernel::create($paramaters['environment'], true);
$serviceKernel->setParameterBag(new ParameterBag($paramaters));
$serviceKernel->setConnection($connection);
include __DIR__ . '/src/functions.php';
$app = new Silex\Application();
include __DIR__ . '/config/' . API_ENV . '.php';
$app->register(new Silex\Provider\ServiceControllerServiceProvider());
$app->view(function (array $result, Request $request) use($app) {
    // 兼容气球云搜索的接口
    $documentType = $request->headers->get('X-Search-Document');
    if ($documentType) {
        $class = "Topxia\\Api\\SpecialResponse\\{$documentType}Response";
        if (!class_exists($class)) {
            throw new \RuntimeException("{$documentType}Response不存在!");
        }
        $obj = new $class();
        $result = $obj->filter($result);
    }
    return new JsonResponse($result);
});
$app->before(function (Request $request) use($app) {
    $auth = new ApiAuth(include __DIR__ . '/config/whitelist.php');
    $auth->auth($request);
});
$app->error(function (\Exception $exception, $code) use($app) {
    $error = array('code' => $code, 'message' => $exception->getMessage());
    if ($app['debug']) {
        if (!$exception instanceof FlattenException) {
            $exception = FlattenException::create($exception);
Exemplo n.º 3
0
<?php

//toDo: Remove hard coded timezone.
use Symfony\Component\HttpFoundation\Request;
ini_set('date.timezone', 'Europe/Berlin');
require_once __DIR__ . '/../vendor/autoload.php';
$app = new Silex\Application();
$app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__));
$productList = [['id' => 4711, 'description' => 'Segelschiff', 'price' => 19.95], ['id' => 4712, 'description' => 'MacBook Air', 'price' => 999.99], ['id' => 4713, 'description' => 'iPod Touch', 'price' => 109.95], ['id' => 4714, 'description' => 'Chromecast', 'price' => 0.99], ['id' => 4715, 'description' => 'Apple TV 2nd generation', 'price' => 179.99], ['id' => 4716, 'description' => 'A glass of wine', 'price' => 7.95], ['id' => 4717, 'description' => 'Toyota Rav4 Hybrid', 'price' => 30000], ['id' => 4718, 'description' => 'PHP Elephant', 'price' => 10.99]];
$app->view(function (array $controllerResult) use($app) {
    return $app->json($controllerResult);
});
$app->get('/products', function (Request $request) use($productList) {
    $term = $request->get('term');
    return array_values(array_filter($productList, function ($product) use($term) {
        if (strpos(strtolower($product['description']), strtolower($term)) !== false) {
            return $product;
        }
    }));
});
$app->get('/', function () use($app) {
    return $app['twig']->render('uiGridCrud.html');
});
$app->run();
Exemplo n.º 4
0
// Define routes for user controller
/** @var \Silex\ControllerCollection $user */
$user = $app['controllers_factory'];
$user->get('/', 'user.controller:getAllUsers');
$user->get('/{user}', 'user.controller:getUser')->convert('user', 'user.converter:convertFromId');
$user->post('/', 'user.controller:createUser')->convert('user', 'user.converter:convertFromRequestBody');
$user->delete('/{user}', 'user.controller:deleteUser')->convert('user', 'user.converter:convertFromId');
// Mount user controller
$app->mount('/users', $user);
// Convert controller responses accordingly
$app->view(function ($controllerResult, \Symfony\Component\HttpFoundation\Request $request) {
    switch ($request->getMethod()) {
        case 'GET':
            if ($controllerResult !== null) {
                return new JsonResponse($controllerResult);
            } else {
                return new Response();
            }
            break;
        case 'PUT':
        case 'POST':
            return new JsonResponse($controllerResult, $status = 201);
            break;
        case 'DELETE':
            return new Response('', $status = 204);
            break;
    }
    return new Response();
});
// Start the application
$app->run();