| New file |
| | |
| | | RewriteEngine On |
| | | |
| | | RewriteCond %{REQUEST_FILENAME} !-f |
| | | RewriteRule (.+) index.php/$1 [L,QSA] |
| | | |
| New file |
| | |
| | | { |
| | | "repositories": [{ |
| | | "type": "vcs", |
| | | "url": "https://github.com/urkle/php-rest-service" |
| | | }], |
| | | "require": { |
| | | "marcj/php-rest-service": "dev-work" |
| | | }, |
| | | "autoload": { |
| | | "psr-0": { "": "src/" } |
| | | } |
| | | } |
| New file |
| | |
| | | <?php |
| | | |
| | | include 'vendor/autoload.php'; |
| | | |
| | | $da = new \DB\DA("sqlite:".__DIR__."/database.sqlite3"); |
| | | |
| | | $server = RestService\Server::create('/') |
| | | ->setClient('\Custom\CorsClient') |
| | | |
| | | ->addSubController('storage', new \RestAPI\Storage($da)) |
| | | ->collectRoutes() |
| | | ->done() |
| | | |
| | | ->run(); |
| | | |
| | | |
| | | // vim: ts=4 et |
| New file |
| | |
| | | <?php |
| | | |
| | | namespace Custom; |
| | | |
| | | class CorsClient extends \RestService\Client { |
| | | public function sendResponse($pHttpCode = '200', $pMessage) |
| | | { |
| | | header("Access-Control-Allow-Origin: *"); |
| | | parent::sendResponse($pHttpCode, $pMessage); |
| | | } |
| | | } |
| | | |
| | | // vim: ts=4 et |
| New file |
| | |
| | | <?php |
| | | |
| | | namespace DB; |
| | | |
| | | class DA { |
| | | private $pdo; |
| | | private $dsn; |
| | | |
| | | function __construct($dsn) |
| | | { |
| | | $this->dsn = $dsn; |
| | | } |
| | | |
| | | private function connect() |
| | | { |
| | | if (empty($this->pdo)) { |
| | | $this->pdo = new \PDO($this->dsn); |
| | | $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
| | | |
| | | $this->ensureSchema(); |
| | | } |
| | | } |
| | | |
| | | const CURRENT_SCHEMA = 1; |
| | | |
| | | private function ensureSchema() |
| | | { |
| | | $ret = $this->pdo->query("PRAGMA user_version"); |
| | | $schema_version = $ret->fetchColumn(0); |
| | | $ret->closeCursor(); |
| | | |
| | | if ($schema_version == self::CURRENT_SCHEMA) return; |
| | | |
| | | if ($schema_version == 0) { |
| | | $this->pdo->exec("CREATE TABLE files (id INTEGER PRIMARY KEY, path VARCHAR(100) NOT NULL, name VARCHAR(30) NOT NULL, size INTEGER NOT NULL, timestamp INTEGER NOT NULL)"); |
| | | $this->pdo->exec("CREATE UNIQUE INDEX files_unique ON files (path ASC, name ASC)"); |
| | | $schema_version += 1; |
| | | } |
| | | |
| | | $this->pdo->exec("PRAGMA user_version = ".self::CURRENT_SCHEMA); |
| | | } |
| | | |
| | | public function getFiles() |
| | | { |
| | | $this->connect(); |
| | | |
| | | $ret = $this->pdo->query("SELECT * FROM files ORDER BY path, name"); |
| | | $ret->setFetchMode(\PDO::FETCH_ASSOC); |
| | | return $ret; |
| | | } |
| | | } |
| | | |
| | | // vim: ts=4 et |
| New file |
| | |
| | | <?php |
| | | |
| | | namespace RestAPI; |
| | | |
| | | class Storage { |
| | | private $da; |
| | | |
| | | function __construct(\DB\DA $da) |
| | | { |
| | | $this->da = $da; |
| | | } |
| | | |
| | | function get() |
| | | { |
| | | return array(); |
| | | } |
| | | |
| | | function getFiles() |
| | | { |
| | | $ret = array(); |
| | | |
| | | $files = $this->da->getFiles(); |
| | | |
| | | foreach( $files as $item) { |
| | | $ret[] = array( |
| | | 'path' => $item['path'] . '/' . $item['name'], |
| | | 'timestamp' => $item['timestamp'], |
| | | 'size' => $item['size'], |
| | | ); |
| | | } |
| | | |
| | | return $ret; |
| | | } |
| | | } |
| | | |
| | | // vim: ts=4 et |