Skip to content

Commit

Permalink
Configurable Rules
Browse files Browse the repository at this point in the history
More Rules
  • Loading branch information
frickelbruder committed Sep 9, 2016
1 parent d01647b commit bdb1f2a
Show file tree
Hide file tree
Showing 18 changed files with 177 additions and 52 deletions.
8 changes: 5 additions & 3 deletions example/security.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
defaults:
target:
host: www.somehost.com
scheme: https://

Sections:
website:
rules:
- HttpHeaderXSSProtection
- HttpHeaderXSSProtectionSecure
- HttpHeaderExposePHP
- HttpHeaderContentTypeNoSniffing
- HttpHeaderCookieWithHttpOnlyFlag


- HttpHeaderHSTSPresent
- HttpHeaderHSTSWithSubdomains
- HttpHeaderCookieWithHttpSecureFlag


3 changes: 2 additions & 1 deletion example/seo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Sections:
uri: pics/someimage.png
rules:
- HttpHeaderResourceFound
- HttpHeaderHasFarFutureExpiresHeader
- HttpHeaderHasFarFutureExpiresHeader:
- ["thresholdInSeconds", 604800]

javascript:
config:
Expand Down
47 changes: 24 additions & 23 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Frickelbruder\KickOff\Configuration;

use Frickelbruder\KickOff\Rules\RuleInterface;
use Frickelbruder\KickOff\Yaml\Yaml;

class Configuration {
Expand Down Expand Up @@ -69,9 +70,6 @@ protected function prepareConfiguredItems($config) {
if( isset( $config['defaults']['target'] ) ) {
$this->buildDefaultTarget( $config['defaults']['target'] );
}
if( isset( $config['Rules'] ) ) {
$this->generateRules( $config['Rules'] );
}
}

private function buildDefaultTarget($config) {
Expand All @@ -86,21 +84,15 @@ private function enrichTarget(TargetUrl $targetUrl, $config) {
}
}

/**
* @param $config
*
* @return RuleInterface[]
*/
private function generateRules($config) {

foreach($config as $name => $values) {
$className = $values['class'];
$class = new $className();
if(isset($values['calls']) && is_array($values['calls'])) {
foreach($values['calls'] as $calls) {
$method = $calls[0];
$params = $calls[1];
call_user_func_array(array($class, $method), $params);
}
}
$this->rules[$name] = $class;
}

$ruleBuilder = new RuleBuilder();
$this->rules = $ruleBuilder->buildRules($config);
return $this->rules;
}

/**
Expand All @@ -111,20 +103,29 @@ protected function buildSections($config) {
$section = new Section( $name );
$sectionTargetUrl = $this->getSectionTargetUrl( $sectionConfig );
$section->setTargetUrlItem( $sectionTargetUrl );
$section->setRules( $this->getRulesForSection( $sectionConfig ) );
$section->setRules( $this->getRulesForSection( $sectionConfig, $config ) );
$this->sections[ $name ] = $section;
}
}

private function getRulesForSection($config) {
if(empty($config['rules'])) {
private function getRulesForSection($sectionConfig, $mainConfig) {
if(empty($sectionConfig['rules'])) {
return array();
}
$result = array();
foreach($config['rules'] as $name) {
$result[$name] = $this->rules[$name];
foreach( $sectionConfig['rules'] as $name) {
$plainName = $name;
if(is_array($name)) {
list($plainName, $configData) = each($name);
foreach($configData as $variableBlock) {
$rule['configuration'][] = array('set', $variableBlock);
}
}
$rule = $mainConfig['Rules'][$name];
$result[$plainName] = $rule;
}
return $result;

return $this->generateRules($result);
}

private function getSectionTargetUrl($config){
Expand Down
46 changes: 46 additions & 0 deletions src/Configuration/RuleBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace Frickelbruder\KickOff\Configuration;

use Frickelbruder\KickOff\Rules\ConfigurableRuleInterface;
use Frickelbruder\KickOff\Rules\Exceptions\RuleNotConfigurableException;

class RuleBuilder {

public function buildRules($config) {
$rules = array();
foreach($config as $name => $values) {
$className = $values['class'];
$class = new $className();
$this->addMethodCalls( $values, $class );
$this->addConfiguration( $values, $class );
$rules[$name] = $class;
}
return $rules;
}

/**
* @param $values
* @param $class
*/
private function addMethodCalls($config, $class) {
if( isset( $config['calls'] ) && is_array( $config['calls'] ) ) {
foreach( $config['calls'] as $calls ) {
$method = $calls[0];
$params = $calls[1];
call_user_func_array( array( $class, $method ), $params );
}
}

}

private function addConfiguration($config, $class) {
if(empty($config['configuration'])) {
return;
}
if(!(isset($config['configuration']) && $class instanceof ConfigurableRuleInterface)) {
throw new RuleNotConfigurableException($config['class'] . ' is not configurable.');
}
$this->addMethodCalls(array('calls' => $config['configuration']), $class);
}

}
8 changes: 4 additions & 4 deletions src/Configuration/Section.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Frickelbruder\KickOff\Configuration;

use Frickelbruder\KickOff\Rules\Rule;
use Frickelbruder\KickOff\Rules\RuleInterface;

class Section {

Expand All @@ -11,7 +11,7 @@ class Section {
protected $name = '';

/**
* @var Rule[]
* @var RuleInterface[]
*/
protected $rules = array();

Expand All @@ -25,7 +25,7 @@ public function __construct($name) {
}

/**
* @param Rule[] $rules
* @param RuleInterface[] $rules
*/
public function setRules($rules) {
$this->rules = $rules;
Expand All @@ -43,7 +43,7 @@ public function getName() {
}

/**
* @return Rule[]
* @return RuleInterface[]
*/
public function getRules() {
return $this->rules;
Expand Down
4 changes: 2 additions & 2 deletions src/Log/Listener/ConsoleOutputListener.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Frickelbruder\KickOff\Log\Listener;

use Frickelbruder\KickOff\Rules\Rule;
use Frickelbruder\KickOff\Rules\RuleInterface;
use Symfony\Component\Console\Output\ConsoleOutput;

class ConsoleOutputListener implements Listener {
Expand All @@ -20,7 +20,7 @@ public function __construct() {
}


public function log($sectionName, $targetUrl, Rule $rule, $success) {
public function log($sectionName, $targetUrl, RuleInterface $rule, $success) {
$output = '.';
if(!$success) {
$this->messages[] = $rule->getErrorMessage() . '(' . $sectionName. ':' . $rule->getName() . ')';
Expand Down
4 changes: 2 additions & 2 deletions src/Log/Listener/JunitLogListener.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
namespace Frickelbruder\KickOff\Log\Listener;

use Frickelbruder\KickOff\Rules\Rule;
use Frickelbruder\KickOff\Rules\RuleInterface;

class JunitLogListener implements Listener {

public $logFileName = 'kickoff.xml';

private $logs = array();

public function log($sectionName, $targetUrl, Rule $rule, $success) {
public function log($sectionName, $targetUrl, RuleInterface $rule, $success) {
$ruleName = $rule->getName();
if(!isset($this->logs[$sectionName])) {
$this->logs[$sectionName] = array();
Expand Down
6 changes: 3 additions & 3 deletions src/Log/Listener/Listener.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php
namespace Frickelbruder\KickOff\Log\Listener;

use Frickelbruder\KickOff\Rules\Rule;
use Frickelbruder\KickOff\Rules\RuleInterface;

Interface Listener {

/**
* @param string $sectionName
* @param string $targetUrl
* @param Rule $rule
* @param RuleInterface $rule
* @param Boolean $success
*
* @return mixed
*/
public function log($sectionName, $targetUrl, Rule $rule, $success);
public function log($sectionName, $targetUrl, RuleInterface $rule, $success);

public function finish();

Expand Down
14 changes: 7 additions & 7 deletions src/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use Frickelbruder\KickOff\Log\Exceptions\ListenerNotFoundException;
use Frickelbruder\KickOff\Log\Listener\Listener;
use Frickelbruder\KickOff\Rules\Rule;
use Frickelbruder\KickOff\Rules\RuleInterface;


class Logger {
Expand Down Expand Up @@ -44,29 +44,29 @@ public function getListener($name) {
/**
* @param string $sectionName
* @param string $targetUrl
* @param Rule $rule
* @param RuleInterface $rule
*
*/
public function logSuccess($sectionName, $targetUrl, Rule $rule) {
public function logSuccess($sectionName, $targetUrl, RuleInterface $rule) {
$this->log($sectionName, $targetUrl, $rule, true);
}

/**
* @param string $sectionName
* @param string $targetUrl
* @param Rule $rule
* @param RuleInterface $rule
*/
public function logFail($sectionName, $targetUrl, Rule $rule) {
public function logFail($sectionName, $targetUrl, RuleInterface $rule) {
$this->log($sectionName, $targetUrl, $rule, false);
}

/**
* @param string $sectionName
* @param string $targetUrl
* @param Rule $rule
* @param RuleInterface $rule
* @param Boolean $success
*/
public function log($sectionName, $targetUrl, Rule $rule, $success) {
public function log($sectionName, $targetUrl, RuleInterface $rule, $success) {
foreach($this->listeners as $listener) {
$listener->log($sectionName, $targetUrl, $rule, $success);
}
Expand Down
21 changes: 21 additions & 0 deletions src/Rules/ConfigurableRuleBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace Frickelbruder\KickOff\Rules;

use Frickelbruder\KickOff\Rules\Exceptions\FieldNotConfigurableException;

abstract class ConfigurableRuleBase extends RuleBase implements ConfigurableRuleInterface {

protected $configurableField = array();

public function set($key, $value) {

if(!in_array($key, $this->configurableField)) {
throw new FieldNotConfigurableException("$key is not configurable");
}

$this->$key = $value;

}


}
8 changes: 8 additions & 0 deletions src/Rules/ConfigurableRuleInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Frickelbruder\KickOff\Rules;

interface ConfigurableRuleInterface extends RuleInterface {

public function set($key, $value);

}
6 changes: 6 additions & 0 deletions src/Rules/Exceptions/FieldNotConfigurableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Frickelbruder\KickOff\Rules\Exceptions;

class FieldNotConfigurableException extends \Exception {

}
6 changes: 6 additions & 0 deletions src/Rules/Exceptions/RuleNotConfigurableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Frickelbruder\KickOff\Rules\Exceptions;

class RuleNotConfigurableException extends \Exception {

}
6 changes: 4 additions & 2 deletions src/Rules/HttpHeaderHasFarFutureExpiresHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use Frickelbruder\KickOff\Rules\Exceptions\HeaderNotFoundException;

class HttpHeaderHasFarFutureExpiresHeader extends RuleBase {
class HttpHeaderHasFarFutureExpiresHeader extends ConfigurableRuleBase {

public $name = 'Far future "Expires" header';

Expand All @@ -12,7 +12,9 @@ class HttpHeaderHasFarFutureExpiresHeader extends RuleBase {
*
* @var integer
*/
private $thresholdInSeconds = 604800;
protected $thresholdInSeconds = 604800;

protected $configurableField = array('thresholdInSeconds');

public function validate() {
try {
Expand Down
4 changes: 3 additions & 1 deletion src/Rules/HttpHeaderHasValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Frickelbruder\KickOff\Rules\Exceptions\HeaderNotFoundException;
use Frickelbruder\KickOff\Rules\Exceptions\InsufficientConfigurationException;

class HttpHeaderHasValue extends RuleBase {
class HttpHeaderHasValue extends ConfigurableRuleBase {

/**
* @var string
Expand All @@ -15,6 +15,8 @@ class HttpHeaderHasValue extends RuleBase {

protected $exactMatch = true;

protected $configurableField = array("value");


public function validate() {
try {
Expand Down
Loading

0 comments on commit bdb1f2a

Please sign in to comment.