Skip to content

Commit

Permalink
refactor: use of a module loading interface
Browse files Browse the repository at this point in the history
  • Loading branch information
noelma committed Feb 13, 2023
1 parent 5fc212f commit 9064ba6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 52 deletions.
9 changes: 7 additions & 2 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ public function getPath(
return $root . Util::cleanPath($dir);
}

/**
* Charge le chemin absolue du projet.
*/
abstract public function getProjectDir(): string;

/**
* Charge les instances des services hors modules.
*/
Expand All @@ -419,9 +424,9 @@ abstract protected function loadServices(): array;
/**
* Charge les instances des contrôleurs dans la table des modules (clé => objet).
*
* @return Controller[]
* @return Module[]
*/
abstract protected function loadModules(): array;
abstract protected function loadModules(): iterable;

/**
* Cherche les routes des modules et les charge dans l'application.
Expand Down
42 changes: 0 additions & 42 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,13 @@
*/
class Controller
{
/**
* Chemin du fichier contenant les routes.
*
* @var string
*/
protected $pathRoutes = '';

/**
* Chemin du fichier contenant les services.
*
* @var string
*/
protected $pathServices = '';

/**
* Chemin du répertoire contenant les vues.
*
* @var string
*/
protected $pathViews = '';

/**
* Chemin du fichier de boot du module
*
* @var string
*/
protected $pathBoot = '';

/**
* Container d'injection de dépendance (CID).
*
Expand Down Expand Up @@ -94,27 +73,6 @@ public function get(string $key)
return $this->container->get($key);
}

/**
* Retourne le chemin du fichier de configuration des routes.
*/
public function getPathRoutes(): string
{
return $this->pathRoutes;
}

/**
* Retourne le chemin du fichier de configuration des services.
*/
public function getPathServices(): string
{
return $this->pathServices;
}

public function getPathBoot(): string
{
return $this->pathBoot;
}

/**
* Retourne une réponse avec le statut 404.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Soosyze;

interface Module
{
/**
* Retourne le chemin absolut du module
*/
public function getModuleDir(): string;

/**
* Retourne le chemin du fichier de configuration des services.
*/
public function getPathServices(): string;

/**
* Retourne le chemin du fichier de configuration des routes.
*/
public function getPathRoutes(): string;
}
11 changes: 7 additions & 4 deletions tests/Resources/App/AppCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

class AppCore extends \Soosyze\App
{
protected function loadModules(): array
public function getProjectDir(): string
{
return [
new TestController()
];
return __DIR__;
}

protected function loadModules(): iterable
{
yield new TestModule();
}

protected function loadServices(): array
Expand Down
4 changes: 0 additions & 4 deletions tests/Resources/App/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

class TestController extends \Soosyze\Controller
{
protected $pathRoutes = __DIR__ . '/config/routes.php';

protected $pathServices = __DIR__ . '/config/services.php';

public function index(): string
{
return 'ok';
Expand Down
23 changes: 23 additions & 0 deletions tests/Resources/App/TestModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Soosyze\Tests\Resources\App;

use Soosyze\Module;

class TestModule implements Module
{
public function getModuleDir(): string
{
return __DIR__;
}

public function getPathServices(): string
{
return __DIR__ . '/config/services.php';
}

public function getPathRoutes(): string
{
return __DIR__ . '/config/routes.php';
}
}

0 comments on commit 9064ba6

Please sign in to comment.