This commit is contained in:
Yuri Kuznetsov
2014-01-17 17:49:58 +02:00
parent e5f9d1245e
commit 596e59d667
12 changed files with 158 additions and 151 deletions

View File

@@ -8,8 +8,6 @@ class Application
private $metadata;
private $container;
private $serviceFactory;
private $slim;
@@ -27,7 +25,6 @@ class Application
date_default_timezone_set('UTC');
$this->serviceFactory = new ServiceFactory($this->container);
$this->slim = $this->container->get('slim');
$this->metadata = $this->container->get('metadata');
@@ -54,11 +51,6 @@ class Application
return $this->log;
}
public function getServiceFactory()
{
return $this->serviceFactory;
}
public function run($name = 'default')
{
$this->routeHooks();
@@ -78,12 +70,11 @@ class Application
{
$container = $this->getContainer();
$slim = $this->getSlim();
$serviceFactory = $this->getServiceFactory();
$auth = new \Espo\Core\Utils\Api\Auth($container->get('entityManager'), $container);
$this->getSlim()->add($auth);
$this->getSlim()->hook('slim.before.dispatch', function () use ($slim, $container, $serviceFactory) {
$this->getSlim()->hook('slim.before.dispatch', function () use ($slim, $container) {
$route = $slim->router()->getCurrentRoute();
$conditions = $route->getConditions();
@@ -122,7 +113,7 @@ class Application
}
try {
$controllerManager = new \Espo\Core\ControllerManager($container, $serviceFactory);
$controllerManager = new \Espo\Core\ControllerManager($container);
$result = $controllerManager->process($controllerName, $actionName, $params, $data, $slim->request());
$container->get('output')->render($result);
} catch (\Exception $e) {
@@ -140,10 +131,9 @@ class Application
protected function initRoutes()
{
$routes = new \Espo\Core\Utils\Route($this->getContainer()->get('config'), $this->getContainer()->get('fileManager'));
$crudList = array_keys( (array) $this->getContainer()->get('config')->get('crud') );
foreach($routes->getAll() as $route) {
foreach ($routes->getAll() as $route) {
$method = strtolower($route['method']);
if (!in_array($method, $crudList)) {
@@ -159,117 +149,7 @@ class Application
$currentRoute->conditions($route['conditions']);
}
}
/* //todo remove when it is tested
$this->getSlim()->get('/', function() {
return $template = "<h1>EspoCRM REST API</h1>";
});
$this->getSlim()->get('/App/user/', function() {
return '{"user":{"modified_by_name":"Administrator","created_by_name":"","id":"1","user_name":"admin","user_hash":"","system_generated_password":"0","pwd_last_changed":"","authenticate_id":"","sugar_login":"1","first_name":"","last_name":"Administrator","full_name":"Administrator","name":"Administrator","is_admin":"1","external_auth_only":"0","receive_notifications":"1","description":"","date_entered":"2013-06-13 12:18:44","date_modified":"2013-06-13 12:19:48","modified_user_id":"1","created_by":"","title":"Administrator","department":"","phone_home":"","phone_mobile":"","phone_work":"","phone_other":"","phone_fax":"","status":"Active","address_street":"","address_city":"","address_state":"","address_country":"","address_postalcode":"","UserType":"","deleted":"0","portal_only":"0","show_on_employees":"1","employee_status":"Active","messenger_id":"","messenger_type":"","reports_to_id":"","reports_to_name":"","email1":"test@letrium.com","email_link_type":"","is_group":"0","c_accept_status_fields":" ","m_accept_status_fields":" ","accept_status_id":"","accept_status_name":""},"preferences":{}}';
});
$this->getSlim()->get('/Metadata/', function() {
return array(
'controller' => 'Metadata',
);
});
$this->getSlim()->get('/Settings/', function() {
return array(
'controller' => 'Settings',
);
})->conditions( array('auth' => false) );
$this->getSlim()->map('/Settings/', function() {
return array(
'controller' => 'Settings',
);
})->via('PATCH');
$this->getSlim()->get('/:controller/layout/:name/', function() {
return array(
'controller' => 'Layout',
'scope' => ':controller',
);
});
$this->getSlim()->put('/:controller/layout/:name/', function() {
return array(
'controller' => 'Layout',
'scope' => ':controller',
);
});
$this->getSlim()->map('/:controller/layout/:name/', function() {
return array(
'controller' => 'Layout',
'scope' => ':controller',
);
})->via('PATCH');
$this->getSlim()->get('/Admin/rebuild/', function() {
return array(
'controller' => 'Admin',
'action' => 'rebuild',
);
});
$this->getSlim()->get('/:controller/:id', function() {
return array(
'controller' => ':controller',
'action' => 'read',
'id' => ':id'
);
});
$this->getSlim()->get('/:controller', function() {
return array(
'controller' => ':controller',
'action' => 'index',
);
});
$this->getSlim()->post('/:controller', function() {
return array(
'controller' => ':controller',
'action' => 'create'
);
});
$this->getSlim()->put('/:controller/:id', function() {
return array(
'controller' => ':controller',
'action' => 'update',
'id' => ':id'
);
});
$this->getSlim()->map('/:controller/:id', function() {
return array(
'controller' => ':controller',
'action' => 'patch',
'id' => ':id'
);
})->via('PATCH');
$this->getSlim()->get('/:controller/:id/:link', function() {
return array(
'controller' => ':controller',
'action' => 'listLinked',
'id' => ':id',
'link' => ':link',
);
});
$this->getSlim()->get('/:controller/:id/:link/:foreignId', function() {
return array(
'controller' => ':controller',
'action' => 'readRelated',
'id' => ':id',
'link' => ':link',
'foreignId' => ':foreignId'
);
}); */
}
}

View File

@@ -99,6 +99,13 @@ class Container
$this->get('slim')
);
}
private function loadServiceFactory()
{
return new \Espo\Core\ServiceFactory(
$this
);
}
private function loadMetadata()
{

View File

@@ -12,16 +12,13 @@ class ControllerManager
private $metadata;
private $container;
private $serviceFactory;
public function __construct(\Espo\Core\Container $container, \Espo\Core\ServiceFactory $serviceFactory)
public function __construct(\Espo\Core\Container $container)
{
$this->container = $container;
$this->config = $this->container->get('config');
$this->metadata = $this->container->get('metadata');
$this->serviceFactory = $serviceFactory;
}
protected function getConfig()
@@ -56,7 +53,7 @@ class ControllerManager
throw new NotFound("Controller '$controllerName' is not found");
}
$controller = new $controllerClassName($this->container, $this->serviceFactory);
$controller = new $controllerClassName($this->container);
if ($actionName == 'index') {
$actionName = $controllerClassName::$defaultAction;

View File

@@ -12,14 +12,11 @@ abstract class Base
private $container;
private $serviceFactory;
public static $defaultAction = 'index';
public function __construct(Container $container, ServiceFactory $serviceFactory)
public function __construct(Container $container)
{
$this->container = $container;
$this->serviceFactory = $serviceFactory;
if (empty($this->name)) {
$name = get_class($this);
@@ -64,7 +61,7 @@ abstract class Base
protected function getServiceFactory()
{
return $this->serviceFactory;
return $this->container->get('serviceFactory');
}
protected function getService($className)

View File

@@ -23,6 +23,8 @@ class HookManager
protected $hookList = array(
'beforeSave',
'afterSave',
'beforeRemove',
'afterRemove',
);
public function __construct(Container $container)
@@ -52,9 +54,8 @@ class HookManager
$this->data = $this->getHookData( array('Espo/Hooks', 'Espo/Custom/Hooks') );
foreach ($metadata->getModuleList() as $moduleName) {
$this->data = array_merge($this->data, $this->getHookData( array('Espo/Modules/'.$moduleName.'/Hooks', 'Espo/Custom/Modules/'.$moduleName.'/Hooks') ));
}
$this->data = array_merge($this->data, $this->getHookData(array('Espo/Modules/'.$moduleName.'/Hooks', 'Espo/Custom/Modules/'.$moduleName.'/Hooks')));
}
if ($this->getConfig()->get('useCache')) {
$this->getFileManager()->setContentPHP($this->data, $this->cacheFile);
@@ -73,7 +74,10 @@ class HookManager
$hook->$hookName($injection);
}
}
}
}
if ($scope != 'Common') {
$this->process('Common', $hookName, $injection);
}
}
public function createHookByClassName($className)
@@ -81,7 +85,7 @@ class HookManager
if (class_exists($className)) {
$hook = new $className();
$dependencies = $hook->getDependencyList();
foreach ($dependencies as $name) {
foreach ($dependencies as $name) {
$hook->inject($name, $this->container->get($name));
}
return $hook;
@@ -100,13 +104,13 @@ class HookManager
{
$hooks = array();
foreach($hookDirs as $hookDir) {
foreach ($hookDirs as $hookDir) {
$fullHookDir = 'application/'.$hookDir;
if (file_exists($fullHookDir)) {
$fileList = $this->getFileManager()->getFileList($fullHookDir, 1, '\.php$', 'file');
foreach($fileList as $scopeName => $hookFiles) {
foreach ($fileList as $scopeName => $hookFiles) {
$hookScopeDirPath = Util::concatPath($hookDir, $scopeName);
@@ -123,7 +127,7 @@ class HookManager
}
//sort hooks by order
foreach($scopeHooks as $hookName => $hookList) {
foreach ($scopeHooks as $hookName => $hookList) {
ksort($hookList);
$sortedHookList = array();

View File

@@ -6,9 +6,7 @@ use \Espo\Core\Interfaces\Injectable;
class Base implements Injectable
{
protected $dependencies = array();
protected $injections = array(
protected $dependencies = array(
'entityManager',
'config',
'metadata',
@@ -16,6 +14,8 @@ class Base implements Injectable
'user',
);
protected $injections = array();
public static $order = 9;
public function __construct()

View File

@@ -35,6 +35,16 @@ class Repository extends \Espo\ORM\Repository
)
);
}
}
public function remove(Entity $entity)
{
$this->getEntityManager()->getHookManager()->process($this->entityName, 'beforeRemove', $entity);
$result = parent::remove($entity);
$this->getEntityManager()->getHookManager()->process($this->entityName, 'afterRemove', $entity);
return $result;
}
public function save(Entity $entity)

View File

@@ -11,7 +11,7 @@ class ServiceFactory
public function __construct(Container $container)
{
$this->container = $container;
}
}
public function createByClassName($className)
{

View File

@@ -0,0 +1,66 @@
<?php
namespace Espo\Hooks\Common;
use Espo\ORM\Entity;
class Stream extends \Espo\Core\Hooks\Base
{
protected $streamService = null;
protected $dependencies = array(
'entityManager',
'config',
'metadata',
'acl',
'user',
);
protected function init()
{
$this->dependencies[] = 'serviceFactory';
}
protected function getServiceFactory()
{
return $this->getInjection('serviceFactory');
}
protected function checkHasStream(Entity $entity)
{
$entityName = $entity->getEntityName();
return $this->getMetadata()->get("scopes.{$entityName}.stream");
}
public function afterRemove(Entity $entity)
{
if ($this->checkHasStream($entity)) {
$this->getStreamService()->unfollowAllUsersFromEntity($entity);
}
}
public function afterSave(Entity $entity)
{
$entityName = $entity->getEntityName();
if ($this->checkHasStream($entity)) {
if (!$entity->getFetchedValue('id')) {
$this->noteCreate($entity);
}
}
}
protected function getStreamService()
{
if (empty($this->streamService)) {
$this->streamService = $this->getServiceFactory()->createByClassName('\\Espo\\Services\\Stream');
}
return $this->streamService;
}
protected function noteCreate(Entity $entity)
{
$this->getStreamService()->noteCreate($entity);
}
}

View File

@@ -39,8 +39,7 @@ abstract class Entity implements IEntity
/**
* @var EntityManager Entity Manager.
*/
protected $entityManager;
protected $entityManager;
public function __construct($defs = array(), EntityManager $entityManager = null)

View File

@@ -310,7 +310,7 @@ class Record extends \Espo\Core\Services\Base
return $idsUpdated;
// TODO update $where
}
}
public function follow($id, $userId = null)
{

View File

@@ -5,6 +5,8 @@ namespace Espo\Services;
use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\NotFound;
use Espo\ORM\Entity;
class Stream extends \Espo\Core\Services\Base
{
protected $dependencies = array(
@@ -34,6 +36,21 @@ class Stream extends \Espo\Core\Services\Base
return $this->injections['metadata'];
}
public function unfollowAllUsersFromEntity(Entity $entity)
{
if (empty($entity->id)) {
return;
}
$pdo = $this->getEntityManager()->getPDO();
$sql = "
DELETE FROM subscription
WHERE
entity_id = " . $pdo->quote($entity->id) . " AND entity_type = " . $pdo->quote($entity->getEntityName()) . "
";
$sth = $pdo->prepare($sql)->execute();
}
public function findUserStream($params = array())
{
$selectParams = array(
@@ -58,9 +75,11 @@ class Stream extends \Espo\Core\Services\Base
}
foreach ($collection as $e) {
if ($e->get('type') == 'Post' && $e->get('parentId') && $e->get('parentType')) {
if ($e->get('parentId') && $e->get('parentType')) {
$entity = $this->getEntityManager()->getEntity($e->get('parentType'), $e->get('parentId'));
$e->set('parentName', $entity->get('name'));
if ($entity) {
$e->set('parentName', $entity->get('name'));
}
}
}
@@ -115,5 +134,33 @@ class Stream extends \Espo\Core\Services\Base
'collection' => $collection,
);
}
protected function loadAssignedUserName(Entity $entity)
{
$user = $this->getEntityManager()->getEntity('User', $entity->get('assignedUserId'));
if ($user) {
$entity->set('assignedUserName', $user->get('name'));
}
}
public function noteCreate(Entity $entity)
{
$note = $this->getEntityManager()->getEntity('Note');
$note->set('type', 'Create');
$note->set('parentId', $entity->id);
$note->set('parentType', $entity->getEntityName());
if ($entity->get('assignedUserId') != $entity->get('createdById')) {
if (!$entity->has('assignedUserName')) {
$this->loadAssignedUserName($entity);
}
$note->set('data', json_encode(array(
'assignedUserId' => $entity->get('assignedUserId'),
'assignedUserName' => $entity->get('assignedUserName'),
)));
}
$this->getEntityManager()->saveEntity($note);
}
}