[event] adding experimental support for user events

This commit is contained in:
Pepijn Over 2015-02-27 17:06:22 +01:00
parent 1123603f64
commit 8505a34292
15 changed files with 287 additions and 14 deletions

View File

@ -13,6 +13,7 @@
"phpmailer/phpmailer": "5.2.6",
"symfony/config": "2.6.*",
"symfony/dependency-injection": "2.6.*",
"symfony/event-dispatcher": "2.6.*",
"symfony/http-foundation": "2.6.*",
"php-pushover/php-pushover": "dev-master",
"twig/twig": "1.*"

60
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "deb9fd4bbeb05c2d3cf1fc7cf51c6f00",
"hash": "d7d015288768a22125a8c66b13a95d41",
"packages": [
{
"name": "php-pushover/php-pushover",
@ -193,6 +193,64 @@
"homepage": "http://symfony.com",
"time": "2015-01-25 04:39:26"
},
{
"name": "symfony/event-dispatcher",
"version": "v2.6.4",
"target-dir": "Symfony/Component/EventDispatcher",
"source": {
"type": "git",
"url": "https://github.com/symfony/EventDispatcher.git",
"reference": "f75989f3ab2743a82fe0b03ded2598a2b1546813"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/f75989f3ab2743a82fe0b03ded2598a2b1546813",
"reference": "f75989f3ab2743a82fe0b03ded2598a2b1546813",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"psr/log": "~1.0",
"symfony/config": "~2.0,>=2.0.5",
"symfony/dependency-injection": "~2.6",
"symfony/expression-language": "~2.6",
"symfony/stopwatch": "~2.3"
},
"suggest": {
"symfony/dependency-injection": "",
"symfony/http-kernel": ""
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.6-dev"
}
},
"autoload": {
"psr-0": {
"Symfony\\Component\\EventDispatcher\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Symfony Community",
"homepage": "http://symfony.com/contributors"
},
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
}
],
"description": "Symfony EventDispatcher Component",
"homepage": "http://symfony.com",
"time": "2015-02-01 16:10:57"
},
{
"name": "symfony/filesystem",
"version": "v2.6.4",

View File

@ -6,6 +6,14 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/sc
<parameters>
<parameter key="config.theme" type="constant">PSM_THEME</parameter>
<parameter key="modules" type="collection">
<parameter>module.config</parameter>
<parameter>module.error</parameter>
<parameter>module.server</parameter>
<parameter>module.user</parameter>
<parameter>module.install</parameter>
</parameter>
<parameter key="path.src" type="constant">PSM_PATH_SRC</parameter>
<parameter key="path.templates">%path.src%templates</parameter>

View File

@ -27,9 +27,16 @@
**/
namespace psm\Module\Config;
use psm\Module\ModuleInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class ConfigModule implements ModuleInterface {
public function load(ContainerBuilder $container) {
}
public function getControllers() {
return array(
'config' => __NAMESPACE__ . '\Controller\ConfigController',

View File

@ -27,9 +27,16 @@
**/
namespace psm\Module\Error;
use psm\Module\ModuleInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class ErrorModule implements ModuleInterface {
public function load(ContainerBuilder $container) {
}
public function getControllers() {
return array(
'error' => __NAMESPACE__ . '\Controller\ErrorController',

View File

@ -27,9 +27,16 @@
**/
namespace psm\Module\Install;
use psm\Module\ModuleInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class InstallModule implements ModuleInterface {
public function load(ContainerBuilder $container) {
}
public function getControllers() {
return array(
'install' => __NAMESPACE__ . '\Controller\InstallController',

View File

@ -27,8 +27,11 @@
**/
namespace psm\Module;
use Symfony\Component\DependencyInjection\ContainerBuilder;
interface ModuleInterface {
public function load(ContainerBuilder $container);
public function getControllers();
}
}

View File

@ -27,9 +27,16 @@
**/
namespace psm\Module\Server;
use psm\Module\ModuleInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class ServerModule implements ModuleInterface {
public function load(ContainerBuilder $container) {
}
public function getControllers() {
return array(
'server' => __NAMESPACE__ . '\Controller\ServerController',

View File

@ -123,6 +123,10 @@ class ProfileController extends AbstractController {
unset($clean['password_repeat']);
$this->db->save(PSM_DB_PREFIX.'users', $clean, array('user_id' => $this->getUser()->getUserId()));
$this->container->get('event')->dispatch(
\psm\Module\User\UserEvents::USER_EDIT,
new \psm\Module\User\Event\UserEvent($this->getUser()->getUserId())
);
if(isset($password)) {
$this->getUser()->changePassword($this->getUser()->getUserId(), $password);
}

View File

@ -253,12 +253,20 @@ class UserController extends AbstractController {
unset($clean['password']); // password update is executed separately
$this->db->save(PSM_DB_PREFIX.'users', $clean, array('user_id' => $user_id));
$this->addMessage(psm_get_lang('users', 'updated'), 'success');
$event = \psm\Module\User\UserEvents::USER_EDIT;
} else {
// add user
$clean['password'] = ''; // password update is executed separately
$user_id = $this->db->save(PSM_DB_PREFIX.'users', $clean);
$this->addMessage(psm_get_lang('users', 'inserted'), 'success');
$event = \psm\Module\User\UserEvents::USER_ADD;
}
$this->container->get('event')->dispatch(
$event,
new \psm\Module\User\Event\UserEvent($user_id, $this->getUser()->getUserId())
);
if(isset($password)) {
$this->getUser()->changePassword($user_id, $password);
}
@ -294,6 +302,12 @@ class UserController extends AbstractController {
$this->db->delete(PSM_DB_PREFIX . 'users', array('user_id' => $id,));
$this->db->delete(PSM_DB_PREFIX.'users_servers', array('user_id' => $id));
$this->container->get('event')->dispatch(
\psm\Module\User\UserEvents::USER_DELETE,
new \psm\Module\User\Event\UserEvent($id, $this->getUser()->getUserId())
);
$this->addMessage(psm_get_lang('users', 'deleted'), 'success');
} catch(\InvalidArgumentException $e) {
$this->addMessage(psm_get_lang('users', 'error_' . $e->getMessage()), 'error');

View File

@ -0,0 +1,51 @@
<?php
/**
* PHP Server Monitor
* Monitor your servers and websites.
*
* This file is part of PHP Server Monitor.
* PHP Server Monitor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PHP Server Monitor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
*
* @package phpservermon
* @author Pepijn Over <pep@peplab.net>
* @copyright Copyright (c) 2008-2015 Pepijn Over <pep@peplab.net>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @link http://www.phpservermonitor.org/
* @since phpservermon 3.2
**/
namespace psm\Module\User\Event;
use Symfony\Component\EventDispatcher\Event;
class UserEvent extends Event {
protected $user_id;
protected $user_id_by;
public function __construct($user_id, $user_id_by = null) {
$this->user_id = $user_id;
$this->user_id_by = $user_id_by;
}
public function getUserId() {
return $this->user_id;
}
public function getUserIdBy() {
return $this->user_id_by;
}
}

View File

@ -0,0 +1,53 @@
<?php
/**
* PHP Server Monitor
* Monitor your servers and websites.
*
* This file is part of PHP Server Monitor.
* PHP Server Monitor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PHP Server Monitor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
*
* @package phpservermon
* @author Pepijn Over <pep@peplab.net>
* @copyright Copyright (c) 2008-2015 Pepijn Over <pep@peplab.net>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @link http://www.phpservermonitor.org/
* @since phpservermon 3.2
**/
namespace psm\Module\User\EventListener;
use psm\Module\User\UserEvents;
use psm\Module\User\Event\UserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return array(
UserEvents::USER_ADD => array('onUserAdd', 0),
UserEvents::USER_EDIT => array('onUserEdit', 0),
UserEvents::USER_DELETE => array('onUserDelete', 0),
);
}
public function onUserAdd(UserEvent $event) {
}
public function onUserEdit(UserEvent $event) {
}
public function onUserDelete(UserEvent $event) {
}
}

View File

@ -0,0 +1,48 @@
<?php
/**
* PHP Server Monitor
* Monitor your servers and websites.
*
* This file is part of PHP Server Monitor.
* PHP Server Monitor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PHP Server Monitor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
*
* @package phpservermon
* @author Pepijn Over <pep@peplab.net>
* @copyright Copyright (c) 2008-2015 Pepijn Over <pep@peplab.net>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @link http://www.phpservermonitor.org/
* @since phpservermon 3.2
**/
namespace psm\Module\User;
final class UserEvents {
/**
* @var string
*/
const USER_ADD = 'user.add';
/**
* @var string
*/
const USER_EDIT = 'user.edit';
/**
* @var string
*/
const USER_DELETE = 'user.delete';
}

View File

@ -27,9 +27,17 @@
**/
namespace psm\Module\User;
use psm\Module\ModuleInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class UserModule implements ModuleInterface {
public function load(ContainerBuilder $container) {
$event = $container->get('event');
$event->addSubscriber(new EventListener\UserSubscriber);
}
public function getControllers() {
return array(
'user' => __NAMESPACE__ . '\Controller\UserController',
@ -38,4 +46,4 @@ class UserModule implements ModuleInterface {
);
}
}
}

View File

@ -51,6 +51,13 @@ class Router {
public function __construct() {
$this->container = $this->buildServiceContainer();
$mods = $this->container->getParameter('modules');
foreach($mods as $mod) {
$mod_loader = $this->container->get($mod);
$mod_loader->load($this->container);
}
}
/**
@ -110,7 +117,7 @@ class Router {
$controller_id = $module_id;
}
$module = $this->getModule($module_id);
$module = $this->container->get('module.' . $module_id);
$controllers = $module->getControllers();
if(!isset($controllers[$controller_id]) || !class_exists($controllers[$controller_id])) {
throw new \InvalidArgumentException('Controller "' . $controller_id . '" is not registered or does not exist.');
@ -138,16 +145,6 @@ class Router {
return $this->container->get($id);
}
/**
* Get a module
* @param string $module_id
* @return \psm\Module\ModuleInterface
* @throws \InvalidArgumentException
*/
protected function getModule($module_id) {
return $this->container->get('module.' . $module_id);
}
/**
* Build a new service container
* @return \Symfony\Component\DependencyInjection\ContainerBuilder