Phred/README.md

150 lines
4 KiB
Markdown
Raw Normal View History

2025-12-09 21:32:51 +00:00
# Phred
A PHP MVC framework intended for projects of all sizes, designed for both solo and team development.
## Requirements
* **PHP**: 8.2+
* **Web Server**: Apache/Nginx (recommended)
* **Package Manager**: Composer
## Installation
Install Phred via Composer:
Refactor M7 module scaffolding, route inclusion, and tests; implement providers discovery; fix URL extension negotiation; clean docs • Add Service Providers loading from config/providers.php and merge with runtime config; ensure AppServiceProvider boots and contributes routes • Create RouteGroups and guard module route includes in routes/web.php; update Kernel to auto-mount module routes and apply provider routes • Implement create:module as a console Command (extends Phred\Console\Command): ◦ Args: name, prefix; Flags: --update-composer, --no-dump ◦ Stable root resolution (dirname(DIR, 2)); robust args/flags handling under ArrayInput ◦ Scaffolds module dirs (Controllers, Views, Templates, Routes, Providers, etc.), ensures Controllers exists, adds .gitkeep ◦ Writes Provider, View, Controller, Template stubs (fix variable interpolation via placeholders) ◦ Appends guarded include snippet to routes/web.php ◦ Optional composer PSR-4 mapping update (+ backup) and optional autoload dump ◦ Prevents providers.php corruption via name validation and existence checks • Add URL extension negotiation middleware tweaks: ◦ Only set Accept for .json (and future .xml), never for none/php ◦ Never override existing Accept header • Add MVC base classes (Controller, APIController, ViewController, View, ViewWithDefaultTemplate); update ViewController signature and View render contract • Add tests: ◦ CreateModuleCommandTest with setup/teardown to snapshot/restore routes/web.php and composer.json; asserts scaffold and PSR-4 mapping ◦ ProviderRouteTest for provider-contributed route ◦ UrlExtensionNegotiationTest sets API_FORMAT=rest and asserts content-type behavior ◦ MvcViewTest validates transformData+render • Fix config/providers.php syntax and add comment placeholder for modules • Update README: M5/M6/M7 docs, MVC examples, template selection conventions, modules section, URL extension negotiation, and module creation workflow • Update MILESTONES.md: mark M6/M7 complete; add M8 task for register:orm; note M12 XML extension support
2025-12-16 22:14:22 +00:00
```bash
composer create-project getphred/phred
```
## Getting Started
2025-12-14 23:10:01 +00:00
### Creating a Module
2025-12-14 23:10:01 +00:00
Phred uses a modular (Django-style) architecture. All your application logic lives inside modules.
2025-12-14 23:10:01 +00:00
To scaffold a new module:
2025-12-14 23:10:01 +00:00
```bash
2026-01-06 17:02:05 +00:00
php phred create:module Shop
2025-12-14 23:10:01 +00:00
```
2026-01-06 17:02:05 +00:00
This will create the module structure under `modules/Shop`, register the service provider, and mount the routes.
2025-12-14 23:10:01 +00:00
After creating a module, update your `composer.json` to include the new namespace:
2025-12-14 23:10:01 +00:00
```json
{
"autoload": {
"psr-4": {
2026-01-06 17:02:05 +00:00
"Modules\\\\Shop\\\\": "modules/Shop/"
}
}
}
```
2025-12-14 23:10:01 +00:00
Then run:
```bash
composer dump-autoload
2025-12-14 23:10:01 +00:00
```
### Running the Application
Start the local development server:
```bash
php phred run
2025-12-14 23:10:01 +00:00
```
2025-12-15 02:09:06 +00:00
The application will be available at `http://localhost:8000`.
2025-12-15 02:09:06 +00:00
2026-01-06 17:02:05 +00:00
## Configuration
Phred uses a `.env` file for configuration. Key settings include:
* `API_FORMAT`: The default API format (`rest` or `jsonapi`).
* `MODULE_NAMESPACE`: The base namespace for your modules (default: `Modules`).
* `APP_DEBUG`: Enable debug mode (`true` or `false`).
* `COMPRESSION_ENABLED`: Enable response compression (Gzip/Brotli).
## CLI Usage (phred)
2025-12-15 02:09:06 +00:00
The `phred` binary provides several utility and scaffolding commands.
2025-12-15 02:09:06 +00:00
### Generators
* `php phred create:module <name>` — Create a new module.
* `php phred create:command <name>` — Create a custom CLI command.
* `php phred create:<module>:controller <name>` — Create a controller.
* `php phred create:<module>:view <name>` — Create a view and template.
* `php phred create:<module>:model <name>` — Create a domain model.
* `php phred create:<module>:migration <name>` — Create a migration.
* `php phred create:<module>:seed <name>` — Create a database seeder.
* `php phred create:<module>:test <name>` — Create a test.
2025-12-15 02:09:06 +00:00
### Database
* `php phred migrate` — Run database migrations.
* `php phred migration:rollback` — Rollback migrations.
* `php phred seed` — Seed the database.
* `php phred db:backup` — Backup the database.
* `php phred db:restore` — Restore the database.
### Testing & Utilities
* `php phred test` — Run tests for the entire project.
* `php phred test:<module>` — Run tests for a specific module.
2026-01-06 17:02:05 +00:00
* `php phred module:list` — List all discovered and registered modules.
* `php phred module:sync-ns` — Sync `composer.json` PSR-4 with `MODULE_NAMESPACE`.
* `php phred list` — List all available commands.
2026-01-06 17:02:05 +00:00
## Routing
Routes are defined in `routes/web.php`, `routes/api.php`, or within your module's `Routes/` directory. Phred uses `nikic/fast-route` for high-performance routing.
### Basic Routes
You can define routes using the `$router` instance:
```php
// routes/web.php
$router->get('/welcome', WelcomeController::class);
$router->post('/submit', SubmitFormController::class);
```
### Route Groups
Groups allow you to share prefixes and middleware:
```php
$router->group(['prefix' => '/api', 'middleware' => 'api'], function ($router) {
$router->get('/users', ListUsersController::class);
$router->get('/users/{id}', ShowUserController::class);
});
```
### Module Auto-Mounting
Phred automatically mounts module routes based on folder name:
- `modules/Shop/Routes/web.php` → mounted at `/shop`
- `modules/Shop/Routes/api.php` → mounted at `/api/shop` (with `api` middleware)
### Route Listing & Caching
View all registered routes:
```bash
php phred route:list
```
In production, cache your routes for maximum performance:
```bash
php phred route:cache
php phred route:clear # To clear cache
```
## Technical Specifications
2025-12-15 02:09:06 +00:00
For detailed information on the framework architecture, service providers, configuration, and MVC components, please refer to:
2025-12-15 02:09:06 +00:00
👉 **[SPECS.md](./SPECS.md)** | [MILESTONES.md](./MILESTONES.md)
## License
Phred is open-source software licensed under the MIT license.