├── .gitignore ├── .prettierrc ├── src ├── Route.php ├── functions.php ├── PageServerEvent.php ├── Post.php ├── Environment.php └── Backend.php ├── README.md └── composer.json /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | composer.lock 3 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "tabWidth": 4, 3 | "semi": true, 4 | "singleQuote": true 5 | } 6 | -------------------------------------------------------------------------------- /src/Route.php: -------------------------------------------------------------------------------- 1 | id = $id; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/functions.php: -------------------------------------------------------------------------------- 1 | params['name'] ?? 'unknownn'); 9 | return [ 10 | 'message' => "Hello " . $name, 11 | ]; 12 | } 13 | ``` 14 | 15 | ## Installation 16 | 17 | See https://github.com/basuke/vite-plugin-sveltekit-php-backend 18 | 19 | ## License 20 | 21 | MIT 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "basuke/sveltekit-php-backend", 3 | "description": "Supplemental package to support SvelteKit PHP Backend Vite Plugin", 4 | "type": "library", 5 | "license": "MIT", 6 | "keywords": [ 7 | "Svelte", 8 | "SvelteKit", 9 | "Vite", 10 | "Backend" 11 | ], 12 | "homepage": "https://github.com/basuke/sveltekit-php-backend", 13 | "autoload": { 14 | "psr-4": { 15 | "Basuke\\SvelteKit\\": "src/" 16 | }, 17 | "files": [ 18 | "src/functions.php" 19 | ] 20 | }, 21 | "authors": [ 22 | { 23 | "name": "Basuke Suzuki", 24 | "email": "basuke@mac.com" 25 | } 26 | ], 27 | "require": { 28 | "php": ">=8.0.0" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/PageServerEvent.php: -------------------------------------------------------------------------------- 1 | params = $decoded['params'] ?? []; 20 | $this->url = $decoded['url'] ?? ''; 21 | $route = $decoded['route']['id'] ?? ''; 22 | } 23 | $this->route = new Route($route); 24 | 25 | if ($post) { 26 | $this->post = new Post(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Post.php: -------------------------------------------------------------------------------- 1 | values); 14 | } 15 | 16 | public function getAll($name) { 17 | return $this->values[$name] ?? null; 18 | } 19 | 20 | public function get($name) { 21 | $values = $this->getAll($name); 22 | return $values && count($values) > 0 ? $values[0] : null; 23 | } 24 | 25 | public function offsetExists(mixed $offset): bool { return $this->getAll($offset); } 26 | public function offsetGet(mixed $offset): mixed { return $this->get($offset); } 27 | public function offsetSet(mixed $offset, mixed $value): void {} 28 | public function offsetUnset(mixed $offset): void {} 29 | } 30 | -------------------------------------------------------------------------------- /src/Environment.php: -------------------------------------------------------------------------------- 1 | load = self::functionInfo('load'); 14 | 15 | foreach (self::Methods as $method) { 16 | $def = self::functionInfo($method); 17 | if ($def) { 18 | $this->endpoints[$method] = $def; 19 | } 20 | } 21 | 22 | global $actions; 23 | if (is_array($actions)) { 24 | foreach ($actions as $action => $callable) { 25 | $def = self::functionInfo($callable); 26 | if ($def) { 27 | $this->actions[$action] = $def; 28 | } 29 | } 30 | } 31 | } 32 | 33 | public static function functionInfo($callable): ?array { 34 | try { 35 | $function = new \ReflectionFunction($callable); 36 | return array_map(function ($param) { 37 | return self::parameterInfo($param); 38 | }, $function->getParameters()); 39 | } catch (\ReflectionException $e) { 40 | return null; 41 | } 42 | } 43 | 44 | public static function parameterInfo(\ReflectionParameter $param): array { 45 | $name = $param->getName(); 46 | $type = strval($param->getType()); 47 | return [$type, $name]; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Backend.php: -------------------------------------------------------------------------------- 1 | run(); 12 | } 13 | } 14 | 15 | private string $namespace; 16 | private Environment $environment; 17 | 18 | public function __construct(string $namespace) { 19 | $this->namespace = $namespace; 20 | $this->environment = new Environment(); 21 | } 22 | 23 | public function run() { 24 | if (!empty($_SERVER['SVELTEKIT_METHOD'])) { 25 | $this->endpoint($_SERVER['SVELTEKIT_METHOD']); 26 | } else if ($_SERVER['REQUEST_METHOD'] === 'GET') { 27 | $this->load(); 28 | } else if ($_SERVER['REQUEST_METHOD'] === 'POST') { 29 | $this->action(); 30 | } else if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { 31 | $this->options(); 32 | } else { 33 | fail(500, ['error' => "Invalid calling convention"]); 34 | } 35 | } 36 | 37 | public function load() { 38 | $method = 'load'; 39 | if (is_callable($method)) { 40 | $event = PageServerEvent::get(); 41 | $result = call_user_func($method, $event); 42 | 43 | json($result); 44 | } else { 45 | fail(500, ['error' => "Function '{$method}' is not defiened or callable"]); 46 | } 47 | } 48 | 49 | public function endpoint(string $method) { 50 | if (is_callable($method)) { 51 | $event = PageServerEvent::get(); 52 | call_user_func($method, $event); 53 | } else { 54 | fail(500, ['error' => "Function '{$method}' is not defiened or callable"]); 55 | } 56 | } 57 | 58 | public function action() { 59 | $action = $_SERVER['SVELTEKIT_ACTION'] ?? 'default'; 60 | global $actions; 61 | if (isset($actions[$action])) { 62 | if (is_callable($actions[$action])) { 63 | $event = PageServerEvent::get(); 64 | call_user_func($actions[$action], $event); 65 | } else { 66 | fail(500, ['error' => "Action '{$action}' is defiened but not callable"]); 67 | } 68 | } else { 69 | fail(500, ['error' => "Action '{$action}' is not defiened"]); 70 | } 71 | } 72 | 73 | public function options() { 74 | json($this->environment); 75 | } 76 | } 77 | --------------------------------------------------------------------------------