Skip to content
This repository has been archived by the owner on Jul 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #10 from wwphp-fb/feature/modulemanager
Browse files Browse the repository at this point in the history
Feature/modulemanager
  • Loading branch information
petk committed Dec 30, 2014
2 parents 28c0c14 + a360ad2 commit 2d5597a
Show file tree
Hide file tree
Showing 11 changed files with 502 additions and 94 deletions.
23 changes: 3 additions & 20 deletions bin/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,12 @@
$loader = require_once __DIR__ . '/../vendor/autoload.php';

use PHPWorldWide\FacebookBot\Bot;
use PHPWorldWide\FacebookBot\Connection\ConnectionParameters;

use Symfony\Component\Yaml\Yaml;

// parse parameters.yml file
$yaml = Yaml::parse(file_get_contents('./parameters.yml'));
use PHPWorldWide\FacebookBot\Config\ConfigReader;

try {
$email = $yaml['facebookbot']['email'];
$password = $yaml['facebookbot']['password'];

$appId = $yaml['facebookbot']['app_id'];
$appSecret = $yaml['facebookbot']['app_secret'];
$accessToken = $yaml['facebookbot']['access_token'];

$groupId = $yaml['facebookbot']['group_id'];
$debug = $yaml['facebookbot']['debug'];

$connectionParameters = new ConnectionParameters($email, $password, $appId, $appSecret, $accessToken, $groupId);
$config = new ConfigReader('./parameters.yml');

$bot = new Bot($connectionParameters);
$bot->getModuleManager()->loadModule('MemberRequest');
$bot->getModuleManager()->loadModule('NewPost');
$bot = new Bot($config);
} catch (\Exception $e) {
// Some error occurred
echo $e->getMessage();
Expand Down
41 changes: 34 additions & 7 deletions parameters.yml.sample
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
# General
facebookbot:
email: email@domain.tld
password: secretpassword
group_id: 314159265
app_id: 123456789
app_secret: d5EhCCp3V021UWoN49Qcvv7gFvh...
access_token: CAACEcBAD6wzVT0dSnpZCbIEbxL... # optional
debug: false
debug: false

connection:
group_id: 314159265
email: email@domain.tld
password: secretpassword
app_id: 123456789
app_secret: d5EhCCp3V021UWoN49Qcvv7gFvh...
access_token: CAACEcBAD6wzVT0dSnpZCbIEbxL... # optional

# Member Request Module
mod-memberrequest:
autoload: true

# New Post Module
mod-newpost:
autoload: true
gistify_comment: |
[admin] Hi, {author}.
Please keep your post readable by using Gist as your codepad. We have created an example based on your code, so others can read it clearly: {gist_link}.
gistify_patterns:
- '%\<\?(php)?%' # start PHP code
- '%\$\w.*\=.*;%' # variable assignment
- '%(if|for|foreach|while|switch)\s*\(.*\)%' # statement
- '%^return(\s+.*)?;%' # return statement
- '%\w\S*\s*\(.*\)\s*;%' # function call
- '%\<\w+\>(.*</\w+>)%' # html tag
- '%(public|protected|private)\s+(static\s+)?\$\w.*;%' # class field
- '%(public|protected|private)\s+(abstract|static\s+)?function\s*\(%' # class method
- '%{|}%' # curly brace
- '%(abstract\s+)?(class|interface)\s+\w+%' # class, interface
- '%^use\s+(\S+);%' # use statement
gistify_minimum_lines: 3
61 changes: 57 additions & 4 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
* @author Peter Kokot
* @author Dennis Degryse
* @since 0.0.1
* @version 0.0.3
* @version 0.0.5
*/

namespace PHPWorldWide\FacebookBot;

use PHPWorldWide\FacebookBot\Config\ConfigReader;

use PHPWorldWide\FacebookBot\Connection\ConnectionManager;
use PHPWorldWide\FacebookBot\Connection\ConnectionParameters;

use PHPWorldWide\FacebookBot\Module\ModuleManager;

Expand All @@ -34,15 +35,25 @@ class Bot
*/
private $moduleManager;

/**
* The configuration reader.
*/
private $config;

/**
* Constructor.
*
* @param ConnectionParameters $connectionParameters
* @param ConfigReader $config The configuration reader.
*/
public function __construct(ConnectionParameters $connectionParameters)
public function __construct(ConfigReader $config)
{
$this->config = $config;
$connectionParameters = $config->get('connection', 'PHPWorldWide\FacebookBot\Connection\ConnectionParameters');

$this->connectionManager = new ConnectionManager($connectionParameters);
$this->moduleManager = new ModuleManager($this->connectionManager);

$this->autoloadModules();
}

/**
Expand All @@ -60,4 +71,46 @@ public function getConnectionManager()
{
return $this->connectionManager;
}

/**
* Loads modules that are autoloaded.
*/
private function autoloadModules()
{
$moduleSections = array_filter(
$this->config->getKeys(),
function ($key) { return strpos($key, 'mod-') === 0; }
);

foreach ($moduleSections as $moduleSection) {
$moduleName = substr($moduleSection, 4);
$moduleConfig = $this->parseModuleConfig($moduleName);

if ($moduleConfig->isAutoload()) {
$this->moduleManager->loadModule($moduleName, $moduleConfig);
}
}
}

/**
* Creates an instance of a module configuration object.
*
* @param string $moduleName The name of the module
* @param array $config The arguments to supply with the module's constructor
*
* @return Module The new instance, which is an implementation of the Module interface.
*
* @throws ModuleException When the given module could not be loaded or when the representing
* class does not implement the Module interface.
*/
private function parseModuleConfig($moduleName)
{
$parts = explode(".", $moduleName);
$name = array_pop($parts);
$parts += [$name . 'Module', $name . 'Config'];
$fqClassName = ModuleManager::MOD_ROOTNS . '\\' . implode('\\', $parts);
$configKey = 'mod-' . strtolower($name);

return $this->config->get($configKey, $fqClassName);
}
}
Loading

0 comments on commit 2d5597a

Please sign in to comment.