/** * Запуск скрипта * * @param array &$ServiceManager объект сервис-менеджера */ function run(array &$ServiceManager) { $shardsConfig = MysqlFactory\getShardsConfig(ServiceManager\getFactory($ServiceManager, 'Mysql')); // Заказчики $CustomerRepository =& ServiceManager\get($ServiceManager, 'CustomerRepository'); foreach ($shardsConfig[CustomerRepository\SHARD_CONFIG] as $shardId => $_) { CustomerRepository\createShard($CustomerRepository, $shardId); } CustomerRepository\syncLastCustomerId($CustomerRepository, 0); // Два тестовых заказчика CustomerRepository\create($CustomerRepository, 'customer_1', 'Петров', '8anbAw4BbuoM'); CustomerRepository\create($CustomerRepository, 'customer_2', 'Васечкин', 'GZx5ixNwYtos'); // Заказы $OrderRepository =& ServiceManager\get($ServiceManager, 'OrderRepository'); OrderRepository\createDatabaseAndTable($OrderRepository); // Два тестовых заказа OrderRepository\create($OrderRepository, 'text', 100); OrderRepository\create($OrderRepository, 'text2', 200); // Исполнители $ExecutorRepository =& ServiceManager\get($ServiceManager, 'ExecutorRepository'); foreach ($shardsConfig[ExecutorRepository\SHARD_CONFIG] as $shardId => $_) { ExecutorRepository\createShard($ExecutorRepository, $shardId); } ExecutorRepository\syncLastExecutorId($ExecutorRepository, 0); // Два тестовых исполнителя ExecutorRepository\create($ExecutorRepository, 'executor_1', 'Иванов', 100, '3urvrPhNvEpZ'); ExecutorRepository\create($ExecutorRepository, 'executor_2', 'Церетели', 200, 'ki22YIk1FR29'); }
/** * Запуск скрипта * * @param array &$ServiceManager объект сервис-менеджера */ function run(array &$ServiceManager) { $MysqlFactory =& ServiceManager\getFactory($ServiceManager, 'Mysql'); $shardsConfig = MysqlFactory\getShardsConfig($MysqlFactory); // Заказчики $CustomerRepository =& ServiceManager\get($ServiceManager, 'CustomerRepository'); $maxCustomerId = 1; foreach ($shardsConfig[CustomerRepository\SHARD_CONFIG] as $shardId => $_) { $Mysql = MysqlFactory\createShard($MysqlFactory, CustomerRepository\SHARD_CONFIG, $shardId); $data = Mysql\query($Mysql, 'SELECT * FROM ' . CustomerRepository\DATABASE_NAME . '.' . CustomerRepository\TABLE_NAME); while (($customerData = Mysql\fetchAssoc($Mysql, $data)) !== false) { $Customer =& Customer\unserializeFromMysql($customerData); CustomerRepository\saveToMemcached($CustomerRepository, $Customer); CustomerRepository\savePasswordHashToMemcached($CustomerRepository, $Customer, $customerData['password_hash']); $maxCustomerId = max($maxCustomerId, Customer\getId($Customer)); } } CustomerRepository\syncLastCustomerId($CustomerRepository, $maxCustomerId); // Исполнители $ExecutorRepository =& ServiceManager\get($ServiceManager, 'ExecutorRepository'); $maxExecutorId = 1; foreach ($shardsConfig[ExecutorRepository\SHARD_CONFIG] as $shardId => $_) { $Mysql =& MysqlFactory\createShard($MysqlFactory, ExecutorRepository\SHARD_CONFIG, $shardId); $data = Mysql\query($Mysql, 'SELECT * FROM ' . ExecutorRepository\TABLE_NAME . '.' . ExecutorRepository\TABLE_NAME); while (($executorData = Mysql\fetchAssoc($Mysql, $data)) !== false) { $Executor =& Executor\unserializeFromMysql($executorData); ExecutorRepository\saveLoginToMemcached($ExecutorRepository, Executor\getLogin($Executor), Executor\getId($Executor)); ExecutorRepository\savePasswordHashToMemcached($ExecutorRepository, $Executor, $executorData['password_hash']); $maxExecutorId = max($maxExecutorId, Executor\getId($Executor)); } } ExecutorRepository\syncLastExecutorId($ExecutorRepository, $maxExecutorId); // Заказы $OrderRepository =& ServiceManager\get($ServiceManager, 'OrderRepository'); foreach (OrderRepository\fetchAll($OrderRepository) as $Order) { OrderRepository\savePriceToMemcached($OrderRepository, Order\getPrice($Order)); } }
/** * Запуск приложения * * @param array &$Application */ function run(array &$Application) { $ServiceManager =& getServiceManager($Application); /** * У нас нет ни dispatch, ни forward, ни event, поэтому далее простой код */ // Определим роутинг $Router =& Router\construct(ServiceManager\get($ServiceManager, 'Config'), ServiceManager\get($ServiceManager, 'Request'), $Application); Router\match($Router); $controllerName = Router\getControllerName($Router); $actionName = Router\getActionName($Router); $routeName = Router\getRouteName($Router); if (getMode($Application) === MODE_WEB) { if ($routeName === 'root') { header('Location: /index', true, 307); return; } $ActiveUser = null; $userData = Session\getActiveUserData(); if (!is_null($userData)) { if ($userData[1] == 'customer') { // Заказчик if ($routeName !== 'customer' && ($routeName !== 'index' || $actionName !== 'logout')) { header('Location: /customer', true, 307); return; } $CustomerRepository =& CustomerRepository\construct(MemcachedFactory\create(ServiceManager\getFactory($ServiceManager, 'Memcached'), 'cache'), ServiceManager\getFactory($ServiceManager, 'Mysql')); $ActiveUser =& CustomerRepository\fetch($CustomerRepository, $userData[0]); } else { // Исполнитель if ($routeName !== 'executor' && ($routeName !== 'index' || $actionName !== 'logout')) { header('Location: /executor', true, 307); return; } $ExecutorRepository =& ServiceManager\get($ServiceManager, 'ExecutorRepository'); $ActiveUser =& ExecutorRepository\fetch($ExecutorRepository, $userData[0]); } Session\regenerate(); } else { if ($routeName !== 'index') { header('Location: /index', true, 307); return; } } ServiceManager\set($ServiceManager, 'ActiveUser', $ActiveUser); } // Создадим контроллер, вызовем action load('Avaritia\\Controller\\' . $controllerName); $controllerNamespace = 'Avaritia\\Controller\\' . $controllerName . '\\'; // У контроллера должен быть конструктор if (!function_exists($controllerNamespace . 'construct')) { trigger_error('У контроллера [' . $controllerName . '] отсутствует конструктор', E_USER_ERROR); } $Controller =& call_user_func_array($controllerNamespace . 'construct', [&$ServiceManager]); $Request =& ServiceManager\get($ServiceManager, 'Request'); // Полное имя функции экшена для вызова $actionFunction = $controllerNamespace . (Request\isXmlHttpRequest($Request) ? 'cmd' . ucfirst($actionName) : $actionName . 'Action'); if (!function_exists($actionFunction)) { header('HTTP/1.0 404 Not Found'); exit; } if (Request\isXmlHttpRequest($Request)) { // ajax запросы просто возвращают массив данных $View =& View\construct(); View\setVariables($View, call_user_func_array($actionFunction, [&$Controller])); View\setRenderStrategy($View, View\RENDER_STRATEGY_JSON); } else { $View =& call_user_func_array($actionFunction, [&$Controller]); } ServiceManager\set($ServiceManager, 'View', $View); echo Response\toString(ServiceManager\get($ServiceManager, 'Response')); }