Skip to content

Commit

Permalink
refactor into service for easier testing (#11)
Browse files Browse the repository at this point in the history
* refactor into service for easier testing
  • Loading branch information
cnizzardini authored Feb 15, 2022
1 parent e5139a7 commit 5383908
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 156 deletions.
161 changes: 11 additions & 150 deletions src/Command/PreloaderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Core\Configure;
use CakePreloader\Preloader;
use SplFileInfo;
use CakePreloader\Exception\PreloadWriteException;
use CakePreloader\PreloaderService;

/**
* Generates a preload file
Expand All @@ -20,18 +20,15 @@
*/
class PreloaderCommand extends Command
{
/**
* @var \CakePreloader\Preloader
*/
private Preloader $preloader;
private PreloaderService $preloaderService;

/**
* PreloaderCommand constructor.
* @param \CakePreloader\PreloaderService $preloaderService PreloaderService
*/
public function __construct()
public function __construct(PreloaderService $preloaderService)
{
parent::__construct();
$this->preloader = new Preloader();
$this->preloaderService = $preloaderService;
}

/**
Expand All @@ -47,156 +44,20 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
$io->out('Generating preloader...');
$io->hr();

$this->cakephp();
$this->packages($args);
$this->app($args);
$this->plugins($args);

$io->info('Preloader Config: ' . (Configure::check('PreloaderConfig') ? 'present' : 'not found'));

$path = $args->getOption('name') ?? Configure::read('PreloaderConfig.name');
$path = !empty($path) ? $path : ROOT . DS . 'preload.php';

$this->preloader->allowCli((bool)$args->getOption('cli'));
$result = $this->preloader->write($path);

if ($result) {
try {
$path = $this->preloaderService->generate($args, $io);
$io->hr();
$io->success('Preload written to ' . $path);
$io->out('You must restart your PHP service for the changes to take effect.');
$io->hr();

return static::CODE_SUCCESS;
}

$io->err('Error encountered writing to ' . $path);

return static::CODE_ERROR;
}

/**
* Loads the CakePHP framework
*
* @return void
*/
private function cakephp(): void
{
$ignorePaths = implode('|', ['src\/Console', 'src\/Command', 'src\/Shell', 'src\/TestSuite']);

$this->preloader->loadPath(CAKE, function (SplFileInfo $file) use ($ignorePaths) {
return !preg_match("/($ignorePaths)/", $file->getPathname());
});
}

/**
* Adds a list of vendor packages
*
* @param \Cake\Console\Arguments $args The command arguments.
* @return void
*/
private function packages(Arguments $args): void
{
$packages = $args->getOption('packages') ?? Configure::read('PreloaderConfig.packages');
if (empty($packages)) {
return;
}

if (is_string($packages)) {
$packages = explode(',', (string)$args->getOption('packages'));
}

if (empty($packages)) {
triggerWarning('Package list is empty');

return;
}

$packages = array_map(
function ($package) {
return ROOT . DS . 'vendor' . DS . $package;
},
$packages
);

$packages = array_filter($packages, function ($package) {
if (file_exists($package)) {
return true;
}
triggerWarning("Package $package could not be located");
});

foreach ($packages as $package) {
$this->preloader->loadPath($package, function (SplFileInfo $file) use ($args) {
if ($args->getOption('phpunit')) {
return true;
}

return !strstr($file->getPath(), '/tests/');
});
}
}

/**
* Adds the users APP into the preloader
*
* @param \Cake\Console\Arguments $args The command arguments.
* @return void
*/
private function app(Arguments $args): void
{
if (($args->hasOption('app') && !$args->getOption('app')) && !Configure::read('PreloaderConfig.app')) {
return;
}

$ignorePaths = ['src\/Console', 'src\/Command'];
if (!$args->getOption('phpunit')) {
$ignorePaths[] = 'tests\/';
}

$ignorePattern = implode('|', $ignorePaths);

$this->preloader->loadPath(APP, function (SplFileInfo $file) use ($ignorePattern) {
return !preg_match("/($ignorePattern)/", $file->getPathname());
});
}

/**
* Adds the users plugins into the preloader
*
* @param \Cake\Console\Arguments $args The command arguments.
* @return void
*/
private function plugins(Arguments $args)
{
$plugins = $args->getOption('plugins') ?? Configure::read('PreloaderConfig.plugins');
if (empty($plugins)) {
return;
}

$paths = [];
if ($plugins === '*' || $plugins === true) {
$paths[] = ROOT . DS . 'plugins';
} elseif (is_string($plugins)) {
$plugins = explode(',', (string)$args->getOption('plugins'));
}

if (is_array($plugins)) {
foreach ($plugins as $plugin) {
$paths[] = ROOT . DS . 'plugins' . DS . $plugin . DS . 'src';
}
}

$ignorePaths = ['src\/Console', 'src\/Command'];
if (!$args->getOption('phpunit')) {
$ignorePaths[] = 'tests\/';
}

$ignorePattern = implode('|', $ignorePaths);
} catch (PreloadWriteException $e) {
$io->err($e->getMessage());

foreach ($paths as $path) {
$this->preloader->loadPath($path, function (SplFileInfo $file) use ($ignorePattern) {
return !preg_match("/($ignorePattern)/", $file->getPathname());
});
return static::CODE_ERROR;
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/Exception/PreloadWriteException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);

namespace CakePreloader\Exception;

use Cake\Core\Exception\CakeException;

/**
* Thrown when a preloader file fails to be written
*/
class PreloadWriteException extends CakeException
{
}
16 changes: 16 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cake\Console\CommandCollection;
use Cake\Core\BasePlugin;
use Cake\Core\Configure;
use Cake\Core\ContainerInterface;
use Cake\Core\PluginApplicationInterface;
use CakePreloader\Command\PreloaderCommand;

Expand Down Expand Up @@ -52,4 +53,19 @@ public function console(CommandCollection $commands): CommandCollection

return $commands;
}

/**
* @inheritDoc
*/
public function services(ContainerInterface $container): void
{
if (PHP_SAPI === 'cli') {
$container
->add(PreloaderService::class);

$container
->add(PreloaderCommand::class)
->addArgument(PreloaderService::class);
}
}
}
Loading

0 comments on commit 5383908

Please sign in to comment.