Exemple #1
0
 public function run()
 {
     $config = $this->app->database;
     $dsn = sprintf('%s:host=%s;port=%s', $config['type'], $config['host'], $config['port']);
     $pdo = new PDO($dsn, $config['username'], $config['password'], $config['options']);
     try {
         if ($this->params['force']) {
             // Force create database
             $pdo->exec('DROP DATABASE `' . $config['dbname'] . '`');
             $pdo->exec('CREATE DATABASE `' . $config['dbname'] . '` DEFAULT CHARACTER SET `utf8mb4`;');
         } else {
             // Try to create database
             $pdo->exec('CREATE DATABASE IF NOT EXISTS `' . $config['dbname'] . '` DEFAULT CHARACTER SET `utf8mb4`;');
         }
         $pdo->exec("USE `{$config['dbname']}`");
         $sql = file_get_contents(APP_DIR . '/migrations/schema.sql');
         $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
         $pdo->exec($sql);
         if ($this->params['data']) {
             $sql = file_get_contents(APP_DIR . '/migrations/data.sql');
             $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
             $pdo->exec($sql);
         }
     } catch (\PDOException $e) {
         print Console::text('Create database error: ' . $e->getMessage() . PHP_EOL, 'red');
         $this->output->end();
     }
     print Console::text('Init database ok' . PHP_EOL, 'green');
 }
Exemple #2
0
 public function run($req, $res)
 {
     $app = (include $this->app->command['app_dir'] . '/bootstrap.php');
     switch ($this->params['command']) {
         case "config":
             $config = array();
             foreach ($app->raw() as $key => $value) {
                 if ($key === 'locals') {
                     break;
                 }
                 $config[$key] = $value;
             }
             echo Ini::encode($config);
             break;
         case "env":
             echo Console::text($app->mode(), 'green', true);
             break;
         case "version":
             echo Console::text(\Pagon\VERSION, 'green', true);
             break;
         case "cleanup":
             if (Console::confirm("确定清除 '.git' 目录?")) {
                 echo "Cleaned";
             }
             break;
         default:
             echo Console::text("未知的操作: {$this->params['command']}", 'redbg', true);
             break;
     }
 }
Exemple #3
0
 public function run($req, $res)
 {
     $port = $this->params['port'] ? $this->params['port'] : '5000';
     $command_app = $this->app;
     $default_headers = array('X-Powered-By' => 'Pagon/0.8');
     /**
      * Mock Application and init
      */
     $app = (include $command_app->command['app_dir'] . '/bootstrap.php');
     $server_app = function ($request, $response) use($port, $app, $command_app, $default_headers) {
         /**
          * Static file check and render
          */
         $static_file = $command_app->command['public_dir'] . '/' . $request->getPath();
         if (is_file($static_file)) {
             echo Console::text("<green>200</green>" . ' <cyan>' . str_pad($request->getMethod(), 6, ' ', STR_PAD_RIGHT) . '</cyan>' . ' ' . $request->getPath(), true);
             $_arr = explode('.', $static_file);
             $ext = end($_arr);
             $mimes = Config::export('mimes');
             $response->writeHead(200, $default_headers + array('Content-Type' => isset($mimes[$ext]) ? $mimes[$ext][0] : mime_content_type($static_file)));
             $response->end(file_get_contents($static_file));
             return;
         }
         $raw = '';
         $app->input = $mock_req = new \Pagon\Http\Input(array('app' => $app));
         $app->output = $mock_res = new \Pagon\Http\Output(array('app' => $app));
         $app->buffer = true;
         $app->cli = false;
         // Init request data
         $mock_req->server = $mock_req->query = $mock_req->data = $mock_req->files = $mock_req->_cookies = array();
         $headers = $request->getHeaders();
         $mock_req->query = $request->getQuery();
         if ($headers['Cookie']) {
             $mock_req->_cookies = decode_cookie($headers['Cookie']);
         }
         $request->on('data', function ($data) use(&$raw, $request, $headers, $app, $command_app) {
             $raw .= $data;
             if (strlen($raw) < $headers['Content-Length']) {
                 return;
             }
             // Start parse
             $parsed = parse_raw_http_request($raw, $headers['Content-Type']);
             // Inject data and files
             $app->input->data = $parsed['data'];
             $app->input->files = $parsed['files'];
             include $command_app->command['public_dir'] . '/index.php';
         });
         /**
          * Web header initial
          */
         foreach ($headers as $k => $v) {
             $mock_req->server['HTTP_' . strtoupper(str_replace('-', '_', $k))] = $v;
         }
         /**
          * Web environment set
          */
         $mock_req->server['REQUEST_URI'] = $request->getPath() . ($mock_req->query ? '?' . http_build_query($mock_req->query) : '');
         $mock_req->server['REQUEST_METHOD'] = $request->getMethod();
         $mock_req->server['REMOTE_ADDR'] = '127.0.0.1';
         $mock_req->server['SERVER_NAME'] = '127.0.0.1';
         $mock_req->server['SERVER_PORT'] = $port;
         $mock_req->server['SCRIPT_NAME'] = '/';
         /**
          * Pagon header inject
          */
         $mock_res->on('header', function () use($response, $request, $mock_res, $mock_req, $default_headers) {
             echo Console::text(($mock_res->status < 400 ? "<green>{$mock_res->status}</green>" : "<red>{$mock_res->status}</red>") . ' <cyan>' . str_pad($request->getMethod(), 6, ' ', STR_PAD_RIGHT) . '</cyan>' . ' ' . $mock_req->url, true);
             $headers = $mock_res->headers;
             if ($cookies = $mock_res->buildCookie()) {
                 $headers['Set-Cookie'] = encode_cookie($cookies);
             }
             try {
                 $response->writeHead($mock_res->status, $default_headers + $headers);
                 $response->end($mock_res->body);
             } catch (\Exception $e) {
                 echo Console::text($e->getMessage(), 'red', true);
             }
             $mock_res->body('');
         });
     };
     $loop = \React\EventLoop\Factory::create();
     $socket = new \React\Socket\Server($loop);
     $http = new \React\Http\Server($socket, $loop);
     $http->on('request', $server_app);
     $socket->listen($port, $this->params['ip']);
     echo "Pagon serve at " . Console::text("http://{$this->params['ip']}:{$port}", array("underline", "bold")) . "\n";
     $loop->run();
 }