Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/maniaplanet/manialive int…
Browse files Browse the repository at this point in the history
…o dev

Conflicts:
	.gitignore
  • Loading branch information
oliverde8 committed Jan 24, 2014
2 parents 8d90614 + c51184d commit 611508f
Show file tree
Hide file tree
Showing 13 changed files with 18 additions and 1,042 deletions.
5 changes: 2 additions & 3 deletions ManiaLive/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ protected function __construct()

// load configuration file
$loader = Loader::getInstance();
$loader->setConfigFilename(APP_ROOT.'config'.DIRECTORY_SEPARATOR.$configFile);
$loader->run();
$loader->setINIConfigFilename(APP_ROOT.'config'.DIRECTORY_SEPARATOR.$configFile);
$loader->load();

// load configureation from the command line ...
CommandLineInterpreter::postConfigLoad();
Expand Down Expand Up @@ -88,7 +88,6 @@ protected function init()
\ManiaLive\Features\EchoHandler::getInstance();
\ManiaLive\Gui\GuiHandler::getInstance();
\ManiaLive\PluginHandler\PluginHandler::getInstance();
\ManiaLive\Threading\ThreadHandler::getInstance();

Dispatcher::dispatch(new Event(Event::ON_INIT));
}
Expand Down
3 changes: 1 addition & 2 deletions ManiaLive/Application/CommandLineInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ static function preConfigLoad()
));

$help = 'ManiaLive '."\n"
.'Authors : '."\n"
.' Philippe "farfa" Melot, Maxime "Gouxim" Raoust, Florian "aseco" Schnell, Gwendal "Newbo.O" Martin'.PHP_EOL
.'Usage: php bootstrapper.php [args]'.PHP_EOL
.'Arguments:'."\n"
.' --help - displays the present help'.PHP_EOL
.' --rpcport=xxx - xxx represents the xmlrpc to use for the connection to the server'.PHP_EOL
.' --address=xxx - xxx represents the address of the server, it should be an IP address or localhost'.PHP_EOL
.' --user=xxx - xxx represents the name of the user to use for the communication. It should be User, Admin or SuperAdmin'.PHP_EOL
.' --password=xxx - xxx represents the password relative to --user Argument'.PHP_EOL
.' --manialive_cfg=xxx - xxx represents the config file for manialive in config/ folder'.PHP_EOL
.' --dedicated_cfg=xxx - xxx represents the name of the Dedicated configuration file to use to get the connection data. This file should be present in the Dedicated\'s config file.'.PHP_EOL
.' --logsPrefix=xxx - xxx reprents the prefix used for this instance log.'.PHP_EOL
.' --debug - activate debug log.'.PHP_EOL;
Expand Down
209 changes: 15 additions & 194 deletions ManiaLive/Config/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,201 +11,22 @@

namespace ManiaLive\Config;

class Loader extends \ManiaLib\Utils\Singleton
class Loader extends \ManiaLib\Application\ConfigLoader
{
static $aliases = array(
'config' => 'ManiaLive\\Config\\Config',
'database' => 'ManiaLive\\Database\\Config',
'wsapi' => 'ManiaLib\\WebServices\\Config',
'manialive' => 'ManiaLive\\Application\\Config',
'server' => 'ManiaLive\\DedicatedApi\\Config',
'threading' => 'ManiaLive\\Threading\\Config',
);

protected $configFilename;
protected $debugPrefix = '[CONFIG LOADER]';

function setConfigFilename($configFilename)
{
$this->configFilename = $configFilename;
}

final public function run()
{
$mtime = microtime(true);
$this->preLoad();
$this->data = $this->load();
$mtime = microtime(true) - $mtime;
$this->postLoad();
}

protected function preLoad()
{
if(!file_exists($this->configFilename))
{
throw new \Exception($this->configFilename.' does not exist');
}
}

protected function postLoad()
{

}

/**
* @return \ManiaLive\Config\Config
*/
protected function load()
{
$values = $this->loadINI($this->configFilename);
list($values, $overrides) = $this->scanOverrides($values);
$values = $this->processOverrides($values, $overrides);
$values = $this->loadAliases($values);
$values = $this->replaceAliases($values);
$instances = $this->arrayToSingletons($values);
return $instances;
}

/**
* @return array
*/
protected function loadINI($filename)
{
try
{
return parse_ini_file($filename, true);
}
catch(Exception $e)
{
throw new Exception('Could not parse INI file: '.$e->getMessage());
}
}

/**
* Creates two arrays (values and ovverides) from one array
* @return array(array values, array overrides)
*/
protected function scanOverrides(array $array)
{
$values = array();
$overrides = array();

foreach($array as $key => $value)
{
if(strstr($key, ':'))
{
$overrides[$key] = $value;
}
else
{
$values[$key] = $value;
}
}
return array($values, $overrides);
}

/**
* Checks if the values from overrides actually match an ovveride rule, anb
* override teh values array if it's the case
* @return array
*/
protected function processOverrides(array $values, array $overrides)
{
if($overrides)
{
foreach($overrides as $key => $override)
{
$matches = null;
if(preg_match('/^hostname: (.+)$/i', $key, $matches))
{
if($matches[1] == gethostname())
{
$values = $this->overrideArray($values, $override);
break;
}
}
}
}
return $values;
}

/**
* Overrides the values of the source array with values from teh overrride array
* It does not work with associate arrays
* @return array
*/
protected function overrideArray(array $source, array $override)
{
foreach($override as $key => $value)
{
$source[$key] = $value;
}
return $source;
}

/**
* @return array
*/
protected function loadAliases(array $values)
{
foreach ($values as $key => $value)
{
if(preg_match('/^\s*alias\s+(\S+)$/i', $key, $matches))
{
if(isset($matches[1]))
{
self::$aliases[$matches[1]] = $value;
unset($values[$key]);
}
}
}
return $values;
}

/**
* @return array
*/
protected function replaceAliases(array $values)
{
$newValues = array();
foreach ($values as $key => $value)
{
$callback = explode('.', $key, 2);
if(count($callback) == 2)
{
$className = reset($callback);
$propertyName = end($callback);
if(isset(self::$aliases[$className]))
{
$className = self::$aliases[$className];
}
$newValues[$className.'.'.$propertyName] = $value;
}
else
{
$newValues[$key] = $value;
}
}
return $newValues;
}

/**
* @return array[Singleton]
*/
protected function arrayToSingletons($values)
{
$instances = array();
foreach($values as $key => $value)
{
$callback = explode('.', $key, 2);
$className = reset($callback);
$propertyName = end($callback);
$instance = call_user_func(array($className, 'getInstance'));

$instance->$propertyName = $value;
$instances[$className] = $instance;
}
return $instances;
function load()
{
$manialiveAliases = array(
'config' => 'ManiaLive\\Config\\Config',
'database' => 'ManiaLive\\Database\\Config',
'wsapi' => 'ManiaLib\\WebServices\\Config',
'manialive' => 'ManiaLive\\Application\\Config',
'server' => 'ManiaLive\\DedicatedApi\\Config',
'threading' => 'ManiaLive\\Threading\\Config'
);

$this->aliases = array_merge($this->aliases, $manialiveAliases);

parent::run();
}
}

Expand Down
21 changes: 0 additions & 21 deletions ManiaLive/Features/WebServices/Config.php

This file was deleted.

26 changes: 0 additions & 26 deletions ManiaLive/PluginHandler/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
use ManiaLive\Features\Tick\Event as TickEvent;
use ManiaLive\PluginHandler\Listener as PluginListener;
use ManiaLive\PluginHandler\Event as PluginEvent;
use ManiaLive\Threading\Listener as ThreadListener;
use ManiaLive\Threading\Event as ThreadEvent;

use ManiaLive\Data\Storage;
use ManiaLive\Database\Connection as DbConnection;
Expand All @@ -50,7 +48,6 @@ abstract class Plugin extends ServerAdapter implements ThreadListener, TickListe
* Event subscriber swichtes
*/
private $eventsApplication = 0;
private $eventsThreading = 0;
private $eventsTick = false;
private $eventsServer = 0;
private $eventsStorage = 0;
Expand Down Expand Up @@ -359,29 +356,6 @@ final protected function disableStorageEvents($events = PlayerEvent::ALL)
$this->eventsStorage &= ~$events;
}

/**
* Starts listening for threading events like:
* onThreadStart, onThreadRestart, onThreadDies, onThreadTimeOut
*/
final protected function enableThreadingEvents($events = ThreadEvent::ALL)
{
$this->restrictIfUnloaded();

Dispatcher::register(ThreadEvent::getClass(), $this, $events & ~$this->eventsThreading);
$this->eventsThreading |= $events;
}

/**
* Stop listening for threading events.
*/
final protected function disableThreadingEvents($events = ThreadEvent::ALL)
{
$this->restrictIfUnloaded();

Dispatcher::unregister(ThreadEvent::getClass(), $this, $events & $this->eventsThreading);
$this->eventsThreading &= ~$events;
}

/**
* Start listen for plugin events like
* onPluginLoaded and onPluginUnloaded
Expand Down
Loading

0 comments on commit 611508f

Please sign in to comment.