Selaa lähdekoodia

Added the core interface for package creation and integration in the Michel framework

michelphp 2 päivää sitten
sitoutus
9585a5cbac
5 muutettua tiedostoa jossa 269 lisäystä ja 0 poistoa
  1. 3 0
      .gitignore
  2. 21 0
      LICENSE
  3. 123 0
      README.md
  4. 20 0
      composer.json
  5. 102 0
      src/PackageInterface.php

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+/vendor/
+/.idea/
+composer.lock

+ 21 - 0
LICENSE

@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2024 F. Michel
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

+ 123 - 0
README.md

@@ -0,0 +1,123 @@
+# Creating an PhpDevCommunity Michel Package
+
+In Michel Framework, you can create packages, which are equivalent to bundles in Symfony. This allows you to organize and share reusable components across different projects. To create an PhpDevCommunity Michel package, you need to implement the `PackageInterface` and define your package's services, parameters, event listeners, routes, and commands.
+
+You can install this library via [Composer](https://getcomposer.org/). Ensure your project meets the minimum PHP version requirement of 7.4.
+
+```bash
+composer require phpdevcommunity/michel-package-starter
+```
+
+## Package Interface
+
+Start by creating a package class that implements the `PackageInterface`. This interface defines the methods you need to implement for your package.
+
+```php
+<?php
+
+namespace PhpDevCommunity\Michel\Package;
+
+interface PackageInterface
+{
+    public function getDefinitions(): array;
+
+    public function getParameters(): array;
+
+    public function getListeners(): array;
+
+    public function getRoutes(): array;
+    
+    public function getControllerSources(): array;
+
+    public function getCommandSources(): array;
+}
+```
+
+## Example Package
+
+Here's an example of an PhpDevCommunity Michel package class (`MyCustomPackage`) that implements the `PackageInterface`. This package provides some default definitions, parameters, and commands. Note that this is just an example; you should create your own package based on your project's requirements.
+
+```php
+final class MyCustomPackage implements PackageInterface
+{
+    public function getDefinitions(): array
+    {
+        // Implement your service definitions here
+        return [];
+    }
+
+    public function getParameters(): array
+    {
+        // Define package-specific parameters here
+        return [];
+    }
+
+    public function getListeners(): array
+    {
+        // Specify event listeners provided by your package
+        return [];
+    }
+
+    public function getRoutes(): array
+    {
+        // Define package-specific routes here
+        return [];
+    }
+    
+    public function getControllerSources(): array
+    {
+        // Define package-specific routes here
+        return [];
+    }
+
+    public function getCommandSources(): array
+    {
+        // List console commands provided by your package
+        return [];
+    }
+}
+```
+
+## Defining Your Package
+
+### Implementing getDefinitions
+
+In the `getDefinitions` method, define the services your package provides. You can use the Dependency Injection Container to create and configure these services.
+
+### Implementing getParameters
+
+The `getParameters` method allows you to define parameters specific to your package. These parameters can be customized in the project's configuration.
+
+### Implementing getListeners
+
+If your package includes event listeners, define them in the `getListeners` method.
+
+### Implementing getRoutes
+
+If your package provides routes, specify them in the `getRoutes` method.
+
+### Implementing getControllerSources
+
+If your package provides controllers, specify them in the `getControllerSources` method.
+
+### Implementing getCommandSources
+
+If your package includes console commands, list them in the `getCommandSources` method.
+
+## Activating Your Package
+
+To activate your package in an PhpDevCommunity Michel project, you need to modify the `packages.php` file located in the `/config` directory. Add your package class to the list of packages along with the environment(s) where it should be active.
+
+```php
+<?php
+
+// /config/packages.php
+
+return [
+    MyCustomPackage::class => ['dev', 'prod'],
+];
+```
+
+In this example, the `MyCustomPackage` is activated for both the 'dev' and 'prod' environments. You can adjust the list of environments as needed.
+
+By following these steps, you can create and activate your PhpDevCommunity Michel packages to extend the functionality of your projects.

+ 20 - 0
composer.json

@@ -0,0 +1,20 @@
+{
+  "name": "michel/michel-package-starter",
+  "description": "The core interface required for creating and integrating packages into the Michel framework.",
+  "type": "library",
+  "require": {
+    "php": ">=7.4",
+    "psr/container": "^1.0|^2.0"
+  },
+  "license": "MIT",
+  "authors": [
+    {
+      "name": "F. Michel"
+    }
+  ],
+  "autoload": {
+    "psr-4": {
+      "Michel\\Package\\": "src"
+    }
+  }
+}

+ 102 - 0
src/PackageInterface.php

@@ -0,0 +1,102 @@
+<?php
+
+namespace Michel\Package;
+/**
+ * Interface PackageInterface
+ *
+ * This interface defines methods for retrieving various package-related configurations and definitions.
+ */
+interface PackageInterface
+{
+    /**
+     * Get the definitions of services for the container.
+     *
+     * Example:
+     * ```
+     * [
+     *     RouterMiddleware::class => static function (ContainerInterface $container) {
+     *         return new RouterMiddleware($container->get('router'), response_factory());
+     *     },
+     *     ExceptionHandler::class => static function (ContainerInterface $container) {
+     *         return new ExceptionHandler(response_factory(), [
+     *             'debug' => $container->get('michel.debug'),
+     *             'html_response' => new HtmlErrorRenderer(
+     *                 response_factory(),
+     *                 $container->get('michel.debug'),
+     *                 $container->get('app.template_dir') . DIRECTORY_SEPARATOR . '_exception'
+     *             ),
+     *         ]);
+     *     },
+     * ]
+     *
+     * @return array An associative array where keys are service identifiers and values are factory functions.
+     */
+    public function getDefinitions(): array;
+
+    /**
+     * Get the parameters configuration.
+     *
+     * Example:
+     * ```
+     * [
+     *     'app.url' => getenv('APP_URL') ?: '',
+     *     'app.locale' => getenv('APP_LOCALE') ?: 'en',
+     *     'app.template_dir' => getenv('APP_TEMPLATE_DIR') ?: '',
+     * ]
+     *
+     * @return array An associative array where keys are parameter names and values are parameter values.
+     */
+    public function getParameters(): array;
+
+    /**
+     * Get the routes configuration.
+     *
+     * @return array An array of `\PhpDevCommunity\Michel\Core\Router\Route` instances defining the routes.
+     */
+    public function getRoutes(): array;
+
+    /**
+     * Return an array of controller sources to scan for attribute-based routes.
+     *
+     * Each source can be either:
+     * - A fully-qualified class name (FQCN), e.g. App\Controller\PingController::class
+     * - A directory path (string), e.g. __DIR__ . '/../src/Controller'
+     *
+     * This allows the router to scan specific controllers or entire folders.
+     *
+     * @return string[] Array of class names and/or absolute folder paths.
+     */
+    public function getControllerSources(): array;
+
+    /**
+     * Get the event listeners configuration.
+     *
+     * Example:
+     * ```
+     * [
+     *     \App\Event\ExampleEvent::class => \App\Listeners\ExampleListener::class,
+     *     // For multiple listeners for the same event:
+     *     // \App\Event\ExampleEvent::class => [
+     *     //     \App\Listeners\ExampleListener::class,
+     *     //     \App\Listeners\ExampleListener2::class,
+     *     //     \App\Listeners\ExampleListener3::class,
+     *     // ]
+     * ]
+     *
+     * @return array An associative array where keys are event class names and values are listener class names or arrays of listener class names.
+     */
+    public function getListeners(): array;
+
+    /**
+     * Return an array of sources to load console commands from.
+     *
+     * Each source can be:
+     * - A fully-qualified class name of a console command
+     * - A directory path (string) to scan for command classes
+     *
+     * This allows both direct registration and dynamic discovery.
+     *
+     * @return string[] Array of FQCNs and/or absolute folder paths.
+     */
+    public function getCommandSources(): array;
+}