Skip to content

Commit

Permalink
BasicAuth: added afterCompile starting
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jun 28, 2017
1 parent d851769 commit 411ff8c
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/DI/BasicAuthExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
namespace Contributte\Http\DI;

use Contributte\Http\Auth\BasicAuthenticator;
use LogicException;
use Nette\DI\CompilerExtension;
use Nette\Http\IRequest;
use Nette\Http\IResponse;
use Nette\PhpGenerator\ClassType;
use Nette\Utils\Validators;

/**
Expand Down Expand Up @@ -35,8 +39,8 @@ public function loadConfiguration()
// Skip if its disabled
if (!$config['enabled']) return;

// Skip if there's no user
if (!$config['users']) return;
// Throws if there's no user
if (!$config['users']) throw new LogicException('You have to define any user or disable extension');

$def = $builder->addDefinition($this->prefix('authenticator'))
->setClass(BasicAuthenticator::class, [$config['title']]);
Expand All @@ -46,4 +50,25 @@ public function loadConfiguration()
}
}

/**
* Decorate initialize
*
* @param ClassType $class
* @return void
*/
public function afterCompile(ClassType $class)
{
$config = $this->validateConfig($this->defaults);

// Skip if its disabled or no user defined
if (!$config['enabled'] || !$config['users']) return;

$initialize = $class->methods['initialize'];
$initialize->addBody('$this->getService(?)->authenticate($this->getByType(?), $this->getByType(?));', [
$this->prefix('authenticator'),
IRequest::class,
IResponse::class,
]);
}

}

0 comments on commit 411ff8c

Please sign in to comment.