diff --git a/apps/core/Module.php b/apps/core/Module.php index 100f310..c9a3c73 100644 --- a/apps/core/Module.php +++ b/apps/core/Module.php @@ -1,5 +1,9 @@ registerNamespaces( + array( + 'Kladr\Core\Models' => $config->application->modelsDir, + 'Kladr\Core\Views' => $config->application->viewsDir, + 'Kladr\Core\Controllers' => $config->application->controllersDir, + 'Kladr\Core\Services' => $config->application->servicesDir, + 'Kladr\Core\Plugins' => $config->application->pluginsDir, + 'Kladr\Core\Plugins\Base' => $config->application->pluginsBaseDir, + 'Kladr\Core\Plugins\General' => $config->application->pluginsGeneralDir, + 'Kladr\Core\Plugins\Tools' => $config->application->pluginsToolsDir, + + ) + ); + + $loader->register(); + } + + /** + * Регистрация сервисов модуля + */ + public function registerServices(\Phalcon\DiInterface $di) + { + $config = new \Phalcon\Config\Adapter\Ini(__DIR__ . '/config/config.ini'); + + // Set site config + $di->set('config', $config); + + // Setting up mongo + $di->set('mongo', function() use ($config) { + $mongo = new \MongoClient($config->database->host, array( + 'connectTimeoutMS' => intval($config->database->timeout), + )); + return $mongo->selectDb($config->database->name); + }, true); + + + // Mongo with users + $di->set('mongoUsers', function() use ($config) { + $mongo = new \MongoClient($config->database->usersHost, array( + 'connectTimeoutMS' => intval($config->database->timeout), + )); + return $mongo->selectDb($config->database->usersName); + }, true); + + // Mongo with users + $di->set('mongoLog', function() use ($config) { + $mongo = new \MongoClient($config->database->logHost, array( + 'connectTimeoutMS' => intval($config->database->timeout), + )); + return $mongo->selectDb($config->database->logName); + }, true); + + // Service for running users + $di->set('userService', '\Kladr\Core\Services\UserService'); + + // Registering the collectionManager service + $di->set('collectionManager', function() { + $modelsManager = new \Phalcon\Mvc\Collection\Manager(); + return $modelsManager; + }, true); + + // Setting up dispatcher + $di->set('dispatcher', function() use ($di) { + $dispatcher = new \Phalcon\Mvc\Dispatcher(); + $dispatcher->setDefaultNamespace('Kladr\Core\Controllers'); + return $dispatcher; + }); + + // Setting memcache + $di->set('memcache', function() use ($config) { + $frontCache = new \Phalcon\Cache\Frontend\Data(array( + "lifetime" => 86400 + )); + $cache = new \Phalcon\Cache\Backend\Memcache($frontCache, array( + "host" => $config->memcache->host, + "port" => $config->memcache->port, + )); + return $cache; + }); + + // Settings mongocache + $di->set('mongocache', function() use ($config) { + $frontCache = new \Phalcon\Cache\Frontend\Data(array( + "lifetime" => 86400 + )); + $cache = new \Phalcon\Cache\Backend\Mongo($frontCache, array( + 'server' => 'mongodb://' . $config->mongocache->host, + 'db' => $config->mongocache->db, + 'collection' => $config->mongocache->collection, + 'connectTimeoutMS' => intval($config->mongocache->timeout), + )); + return $cache; + }); + + // Setting cache + $di->set('cache', array( + 'className' => '\Kladr\Core\Plugins\Tools\Cache', + 'properties' => array( + array( + 'name' => 'cache', + 'value' => array('type' => 'service', 'name' => 'mongocache') + ), + array( + 'name' => 'config', + 'value' => array('type' => 'service', 'name' => 'config') + ) + ) + )); + + //setting sphinxapi + $di->set('sphinxapi', function() use ($config) { + include (dirname(__FILE__) . '/plugins/tools/sphinxapi.php'); + $sphinxapi = new \SphinxClient(); + $sphinxapi->SetServer($config->sphinxapi->server, $config->sphinxapi->port); + return $sphinxapi; + } + ); + + // Register validate plugin + $di->set('validate', function() { + return new \Kladr\Core\Plugins\General\ValidatePlugin(); + }); + + // Register oneString plugin + $di->set('oneString', array( + 'className' => '\Kladr\Core\Plugins\General\OneStringPlugin', + 'properties' => array( + array( + 'name' => 'cache', + 'value' => array('type' => 'service', 'name' => 'cache') + ), + array( + 'name' => 'sphinxClient', + 'value' => array('type' => 'service', 'name' => 'sphinxapi') + ) + ) + )); + + // Register find plugin + $di->set('find', array( + 'className' => '\Kladr\Core\Plugins\General\FindPlugin', + 'properties' => array( + array( + 'name' => 'cache', + 'value' => array('type' => 'service', 'name' => 'cache') + ) + ) + )); + + // Register special cases plugin + $di->set('specialCases', array( + 'className' => '\Kladr\Core\Plugins\General\SpecialCasesPlugin', + 'properties' => array( + array( + 'name' => 'cache', + 'value' => array('type' => 'service', 'name' => 'cache') + ) + ) + )); + + // Register duplicate plugin + $di->set('duplicate', array( + 'className' => '\Kladr\Core\Plugins\General\DuplicatePlugin', + 'properties' => array( + array( + 'name' => 'cache', + 'value' => array('type' => 'service', 'name' => 'cache') + ) + ) + )); + + // Register find parents plugin + $di->set('findParents', array( + 'className' => '\Kladr\Core\Plugins\General\FindParentsPlugin', + 'properties' => array( + array( + 'name' => 'cache', + 'value' => array('type' => 'service', 'name' => 'cache') + ) + ) + )); + + // Register find parents plugin + $di->set('parentsSpecialCases', array( + 'className' => '\Kladr\Core\Plugins\General\ParentsSpecialCasesPlugin', + 'properties' => array( + array( + 'name' => 'cache', + 'value' => array('type' => 'service', 'name' => 'cache') + ) + ) + )); + + // Register find parents plugin + $di->set('logPaidUsersPlugin', array( + 'className' => '\Kladr\Core\Plugins\General\LogPaidUsersPlugin', + 'properties' => array( + array( + 'name' => 'userService', + 'value' => array('type' => 'service', 'name' => 'userService') + ) + ) + )); + + $di->set('allDataPlugin', array( + 'className' => '\Kladr\Core\Plugins\General\AllDataPlugin', + 'properties' => array( + array( + 'name' => 'userService', + 'value' => array('type' => 'service', 'name' => 'userService') + ), + array( + 'name' => 'cacheDir', + 'value' => array('type' => 'parameter', 'value' => $config->application->cacheDir) + ), + array( + 'name' => 'disablePaid', + 'value' => array('type' => 'parameter', 'value' => $config->application->disablePaid) + ) + ) + )); + + $di->set('enabledTokensPlugin', '\Kladr\Core\Plugins\General\EnabledTokensPlugin'); + + // Register GA + $di->set('apiTracker', function() use($config) { + if($config->ga->code == '') + return false; + + return new \Racecore\GATracking\GATracking($config->ga->code); + }); + + // Setting api + $di->setShared('api', function() use ($di, $config) { + $api = new Services\ApiService($di->get('apiTracker')); + + if($config->application->enableTokens) + $api->addPlugin($di->get('enabledTokensPlugin')); + + if($config->application->enableUserLog) + $api->addPlugin($di->get('logPaidUsersPlugin')); + + $api->addPlugin($di->get('allDataPlugin')); + $api->addPlugin($di->get('validate')); + + if($config->sphinxapi->enabled) + $api->addPlugin($di->get('oneString')); + + $api->addPlugin($di->get('find')); + //$api->addPlugin($di->get('specialCases')); + //$api->addPlugin($di->get('duplicate')); + $api->addPlugin($di->get('findParents')); + $api->addPlugin($di->get('parentsSpecialCases')); + return $api; + }); + + // Setting up the view component + $di->set('view', function() use ($config) { + $view = new \Phalcon\Mvc\View(); + $view->setViewsDir($config->application->viewsDir); + return $view; + }); + } + + } + +} \ No newline at end of file diff --git a/apps/frontend/Module.php b/apps/frontend/Module.php index 0b14f77..b530391 100644 --- a/apps/frontend/Module.php +++ b/apps/frontend/Module.php @@ -1,5 +1,9 @@ registerNamespaces( + array( + 'Kladr\Frontend\Models' => $config->application->modelsDir, + 'Kladr\Frontend\Views' => $config->application->viewsDir, + 'Kladr\Frontend\Controllers' => $config->application->controllersDir, + 'Kladr\Frontend\Plugins' => $config->application->pluginsDir, + 'Kladr\Frontend\Library' => $config->application->libraryDir, + 'Phalcon' => __DIR__ .'/vendor/Phalcon/' + ) + ); + + $loader->register(); + } + + /** + * Регистрация сервисов модуля + */ + public function registerServices(\Phalcon\DiInterface $di) + { + $config = new \Phalcon\Config\Adapter\Ini(__DIR__ . '/config/config.ini'); + + // Set site config + $di->set('config', $config); + + // Setting up mongo + $di->set('mongo', function() use ($config) { + $mongo = new \MongoClient($config->database->host); + return $mongo->selectDb($config->database->name); + }, true); + + // Registering the collectionManager service + $di->set('collectionManager', function() { + $modelsManager = new \Phalcon\Mvc\Collection\Manager(); + return $modelsManager; + }, true); + + // Start the session the first time when some component request the session service + $di->set('session', function() use ($config) { + + if(isset($config->session->adapter)) + switch($config->session->adapter){ + case 'mongo' : + $mongo = new \Mongo($config->session->mongoHost); + + $session = new \Phalcon\Session\Adapter\Mongo(array( + 'collection' => $mongo->kladrapiSession->data + )); + break; + + case 'file': + $session = new \Phalcon\Session\Adapter\Files(); + break; + } + else + $session = new \Phalcon\Session\Adapter\Files(); + + + $session->start(); + return $session; + }); + + // Setting up dispatcher + $di->set('dispatcher', function() use ($di) { + + $evManager = $di->getShared('eventsManager'); + $evManager->attach( + "dispatch:beforeException", + function($event, $dispatcher, $exception) + { + switch ($exception->getCode()) { + case \Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: + case \Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND: + $dispatcher->forward( + array( + 'controller' => 'index', + 'action' => 'show404', + ) + ); + return false; + } + } + ); + + $dispatcher = new \Phalcon\Mvc\Dispatcher(); + $dispatcher->setDefaultNamespace("Kladr\Frontend\Controllers"); + $dispatcher->setEventsManager($evManager); + return $dispatcher; + }); + + // Register an user component + $di->set('elements', function() { + return new Library\Elements(); + }); + + // Register key tools + $di->set('keyTools', function() { + return new Plugins\KeyTools(); + }); + + // Register the flash service with custom CSS classes + $di->set('flash', function() { + $flash = new \Phalcon\Flash\Direct(); + return $flash; + }); + + // Setting up the view component + $di->set('view', function() { + $view = new \Phalcon\Mvc\View(); + $view->setViewsDir('../apps/frontend/views/'); + return $view; + }); + + + } + + } + +} \ No newline at end of file diff --git a/public/index.php b/public/index.php index b3d0b92..942ed44 100644 --- a/public/index.php +++ b/public/index.php @@ -34,6 +34,10 @@ try { + $phalconVersion = \Phalcon\Version::get(); + $major = $phalconVersion[0]; + $major = $major == '2' ? '2' : ''; + //Create an application $application = new Application($di); @@ -42,11 +46,11 @@ array( 'frontend' => array( 'className' => 'Kladr\Frontend\Module', - 'path' => '../apps/frontend/Module.php', + 'path' => '../apps/frontend/Module' . $major . '.php', ), 'core' => array( 'className' => 'Kladr\Core\Module', - 'path' => '../apps/core/Module.php', + 'path' => '../apps/core/Module' . $major . '.php', ) ) );