Skip to content

Commit

Permalink
Merge pull request #7 from tattersoftware/register
Browse files Browse the repository at this point in the history
Reinstate handler registration
  • Loading branch information
MGatner authored Sep 14, 2020
2 parents e2d9f71 + 87c7c16 commit 3bf7893
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/analyze.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# When a PR is opened or a push is made, perform
# a static analysis check on the code using PHPStan.
name: PHPStan

on:
pull_request:
branches:
- 'develop'
paths:
- 'src/**'
- 'tests/**'
- 'phpstan*'
- '.github/workflows/analyze.yml'
push:
branches:
- 'develop'
paths:
- 'src/**'
- 'tests/**'
- 'phpstan*'
- '.github/workflows/analyze.yml'

jobs:
build:
name: Analyze code (PHPStan)
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: intl

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Create PHPStan result cache directory
run: mkdir -p build/phpstan

- name: Cache PHPStan result
uses: actions/cache@v2
with:
path: build/phpstan
key: ${{ runner.os }}-phpstan-${{ github.sha }}
restore-keys: ${{ runner.os }}-phpstan-

- name: Install dependencies
run: composer install --no-progress --no-suggest --no-interaction --prefer-dist --optimize-autoloader
env:
COMPOSER_AUTH: ${{ secrets.COMPOSER_AUTH }}

- name: Run static analysis
run: vendor/bin/phpstan analyze
45 changes: 45 additions & 0 deletions src/Commands/HandlersRegister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php namespace Tatter\Handlers\Commands;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use Tatter\Handlers\Handlers;

class HandlersRegister extends BaseCommand
{
protected $group = 'Handlers';
protected $name = 'handlers:register';
protected $description = 'Regsiter all discovered handlers';
protected $usage = 'handlers:register';

public function run(array $params = [])
{
// Load the library
$handlers = new Handlers();

// Make sure auto-discovery is enabled
if (empty($handlers->getConfig()->autoDiscover))
{
CLI::write('No paths are set for automatic discovery. See the config file for Tatter\Handlers.', 'yellow');
return;
}

// Process each path
foreach ($handlers->getConfig()->autoDiscover as $path)
{
$handlers->setPath($path);
CLI::write($path, 'black', 'light_gray');

if (! $classes = $handlers->all())
{
CLI::write('No new handlers registered.', 'yellow');
continue;
}

// Display each class
foreach ($classes as $class)
{
CLI::write($class);
}
}
}
}
36 changes: 36 additions & 0 deletions src/Handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,38 @@ public function all(): array
return $classes;
}

/**
* Iterates through discovered handlers and attempts to register them.
*
* @return array<string> Array of newly-registered classes
*/
public function register(): array
{
$this->discoverHandlers();

if (empty($this->discovered))
{
return [];
}

service('timer')->start('Register ' . $this->path);

$classes = [];
foreach ($this->discovered as $class => $attributes)
{
$instance = new $class();

if (is_callable([$instance, 'register']) && $instance->register())
{
$classes[] = $class;
}
}

service('timer')->stop('Discover ' . $this->path);

return $classes;
}

//--------------------------------------------------------------------

/**
Expand Down Expand Up @@ -211,6 +243,8 @@ protected function discoverHandlers(): self
return $this;
}

service('timer')->start('Discover ' . $this->path);

// Have to do this the hard way
$locator = Services::locator();

Expand Down Expand Up @@ -243,6 +277,8 @@ protected function discoverHandlers(): self
// Cache the results
$this->cacheCommit();

service('timer')->stop('Discover ' . $this->path);

return $this;
}

Expand Down
5 changes: 5 additions & 0 deletions tests/_support/Factories/PopFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public function process()
{
return 'pop';
}

public function register()
{
session()->set(['didRegister' => true]);
}
}
7 changes: 7 additions & 0 deletions tests/unit/LibraryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,11 @@ public function testFirstResetsCriteria()

$this->assertEquals([], $result);
}

public function testRegisterCallsHandlerRegister()
{
$this->handlers->register();

$this->assertEquals(true, session('didRegister'));
}
}

0 comments on commit 3bf7893

Please sign in to comment.