From b55c86267df3c54581bbe57db3ce15de3388230a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Mo=CC=88ller?= Date: Thu, 20 Nov 2014 16:24:06 +0100 Subject: [PATCH] Enhancement: Allow to load module classes with PSR-0 autoloader --- module/Application/Module.php | 38 +-------- module/Application/src/Application/Module.php | 44 ++++++++++ module/User/Module.php | 73 ++-------------- module/User/src/User/Module.php | 65 ++++++++++++++ module/ZfModule/Module.php | 77 +---------------- module/ZfModule/src/ZfModule/Module.php | 84 +++++++++++++++++++ 6 files changed, 205 insertions(+), 176 deletions(-) create mode 100644 module/Application/src/Application/Module.php create mode 100644 module/User/src/User/Module.php create mode 100644 module/ZfModule/src/ZfModule/Module.php diff --git a/module/Application/Module.php b/module/Application/Module.php index ec564230..bd11c555 100644 --- a/module/Application/Module.php +++ b/module/Application/Module.php @@ -2,43 +2,9 @@ /** * Zend Framework (http://framework.zend.com/) * - * @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository + * @link http://github.com/zendframework/ZendSkeletonModule for the canonical source repository * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ -namespace Application; - -use Zend\Mvc\ModuleRouteListener; - -class Module -{ - public function onBootstrap($e) - { - $e->getApplication()->getServiceManager()->get('translator'); - $eventManager = $e->getApplication()->getEventManager(); - $moduleRouteListener = new ModuleRouteListener(); - $moduleRouteListener->attach($eventManager); - } - - public function getConfig() - { - return include __DIR__ . '/config/module.config.php'; - } - - public function getAutoloaderConfig() - { - return array( - 'Zend\Loader\ClassMapAutoloader' => array( - __DIR__ . '/autoload_classmap.php', - ), - 'Zend\Loader\StandardAutoloader' => array( - 'namespaces' => array( - __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, - ), - ), - ); - } - - -} +require_once __DIR__ . '/src/Application/Module.php'; diff --git a/module/Application/src/Application/Module.php b/module/Application/src/Application/Module.php new file mode 100644 index 00000000..5351c1d1 --- /dev/null +++ b/module/Application/src/Application/Module.php @@ -0,0 +1,44 @@ +getApplication()->getServiceManager()->get('translator'); + $eventManager = $e->getApplication()->getEventManager(); + $moduleRouteListener = new ModuleRouteListener(); + $moduleRouteListener->attach($eventManager); + } + + public function getConfig() + { + return include __DIR__ . '/../../config/module.config.php'; + } + + public function getAutoloaderConfig() + { + return array( + 'Zend\Loader\ClassMapAutoloader' => array( + __DIR__ . '/../../autoload_classmap.php', + ), + 'Zend\Loader\StandardAutoloader' => array( + 'namespaces' => array( + __NAMESPACE__ => __DIR__, + ), + ), + ); + } + + +} diff --git a/module/User/Module.php b/module/User/Module.php index 84f7e559..6a0785c8 100644 --- a/module/User/Module.php +++ b/module/User/Module.php @@ -1,65 +1,10 @@ getEventManager()->getSharedManager(); - $sm = $app->getServiceManager(); - - $em->attach('ScnSocialAuth\Authentication\Adapter\HybridAuth','githubToLocalUser', function ($e) { - $localUser = $e->getTarget(); - $userProfile = $e->getParam('userProfile'); - $nickname = substr( - $userProfile->profileURL, - (strrpos($userProfile->profileURL, "/") + 1) - ); - $localUser->setUsername($nickname); - $localUser->setPhotoUrl($userProfile->photoURL); - }); - - $em->attach('EdpGithub\Client', 'api', function ($e) use ($sm) { - $hybridAuth = $sm->get('HybridAuth'); - $adapter = $hybridAuth->getAdapter('github'); - - if($adapter->isUserConnected()) { - $token = $adapter->getAccessToken(); - $client = $e->getTarget(); - $client->authenticate('url_token', $token['access_token']); - } - } ); - } - - public function getServiceConfig() - { - return array( - 'factories' => array( - 'zfcuser_user_mapper' => function ($sm) { - $options = $sm->get('zfcuser_module_options'); - $mapper = new Mapper\User(); - $mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter')); - $entityClass = $options->getUserEntityClass(); - $mapper->setEntityPrototype(new $entityClass); - $mapper->setHydrator(new Mapper\UserHydrator()); - return $mapper; - }, - ), - ); - } - - public function getDir() - { - return __DIR__; - } - - public function getNamespace() - { - return __NAMESPACE__; - } -} +/** + * Zend Framework (http://framework.zend.com/) + * + * @link http://github.com/zendframework/ZendSkeletonModule for the canonical source repository + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License + */ + +require_once __DIR__ . '/src/User/Module.php'; diff --git a/module/User/src/User/Module.php b/module/User/src/User/Module.php new file mode 100644 index 00000000..46e49f48 --- /dev/null +++ b/module/User/src/User/Module.php @@ -0,0 +1,65 @@ +getEventManager()->getSharedManager(); + $sm = $app->getServiceManager(); + + $em->attach('ScnSocialAuth\Authentication\Adapter\HybridAuth','githubToLocalUser', function ($e) { + $localUser = $e->getTarget(); + $userProfile = $e->getParam('userProfile'); + $nickname = substr( + $userProfile->profileURL, + (strrpos($userProfile->profileURL, "/") + 1) + ); + $localUser->setUsername($nickname); + $localUser->setPhotoUrl($userProfile->photoURL); + }); + + $em->attach('EdpGithub\Client', 'api', function ($e) use ($sm) { + $hybridAuth = $sm->get('HybridAuth'); + $adapter = $hybridAuth->getAdapter('github'); + + if($adapter->isUserConnected()) { + $token = $adapter->getAccessToken(); + $client = $e->getTarget(); + $client->authenticate('url_token', $token['access_token']); + } + } ); + } + + public function getServiceConfig() + { + return array( + 'factories' => array( + 'zfcuser_user_mapper' => function ($sm) { + $options = $sm->get('zfcuser_module_options'); + $mapper = new Mapper\User(); + $mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter')); + $entityClass = $options->getUserEntityClass(); + $mapper->setEntityPrototype(new $entityClass); + $mapper->setHydrator(new Mapper\UserHydrator()); + return $mapper; + }, + ), + ); + } + + public function getDir() + { + return __DIR__ . '/../../'; + } + + public function getNamespace() + { + return __NAMESPACE__; + } +} diff --git a/module/ZfModule/Module.php b/module/ZfModule/Module.php index 092d9474..f639a75d 100644 --- a/module/ZfModule/Module.php +++ b/module/ZfModule/Module.php @@ -7,79 +7,4 @@ * @license http://framework.zend.com/license/new-bsd New BSD License */ -namespace ZfModule; - -use Zend\ModuleManager\Feature\AutoloaderProviderInterface; -use Zend\Mvc\ModuleRouteListener; -use Zend\Cache\StorageFactory; - -class Module implements AutoloaderProviderInterface -{ - public function getAutoloaderConfig() - { - return array( - 'Zend\Loader\ClassMapAutoloader' => array( - __DIR__ . '/autoload_classmap.php', - ), - 'Zend\Loader\StandardAutoloader' => array( - 'namespaces' => array( - // if we're in a namespace deeper than one level we need to fix the \ in the path - __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__), - ), - ), - ); - } - - public function getConfig() - { - return include __DIR__ . '/config/module.config.php'; - } - - public function onBootstrap($e) - { - // You may not need to do this if you're doing it elsewhere in your - // application - $eventManager = $e->getApplication()->getEventManager(); - $moduleRouteListener = new ModuleRouteListener(); - $moduleRouteListener->attach($eventManager); - } - - public function getServiceConfig() - { - return array( - 'factories' => array( - 'zfmodule_cache' => function ($sm) { - $config = $sm->get('Config'); - $storage = StorageFactory::factory($config['zfmodule']['cache']); - - return $storage; - }, - 'zfmodule_mapper_module' => function ($sm) { - $mapper = new Mapper\Module(); - $mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter')); - $mapper->setEntityPrototype(new Entity\Module); - $mapper->setHydrator(new Mapper\ModuleHydrator()); - return $mapper; - }, - 'zfmodule_service_module' => function ($sm) { - $service = new Service\Module; - return $service; - }, - 'zfmodule_service_repository' => function ($sm) { - $service = new Service\Repository; - $service->setApi($sm->get('EdpGithub\Client')); - return $service; - }, - /*'github_client' => function ($sm) { - $hybridAuth = $sm->get('HybridAuth'); - $adapter = $hybridAuth->getAdapter('github'); - $token = $adapter->getAccessToken(); - - $client = $sm->get('EdpGithubClient'); - $client->authenticate('url_token',$token['access_token'], null); - return $client; - }*/ - ), - ); - } -} +require_once __DIR__ . '/src/ZfModule/Module.php'; diff --git a/module/ZfModule/src/ZfModule/Module.php b/module/ZfModule/src/ZfModule/Module.php new file mode 100644 index 00000000..4aba31ce --- /dev/null +++ b/module/ZfModule/src/ZfModule/Module.php @@ -0,0 +1,84 @@ + array( + __DIR__ . '/../../autoload_classmap.php', + ), + 'Zend\Loader\StandardAutoloader' => array( + 'namespaces' => array( + __NAMESPACE__ => __DIR__, + ), + ), + ); + } + + public function getConfig() + { + return include __DIR__ . '/../../config/module.config.php'; + } + + public function onBootstrap($e) + { + // You may not need to do this if you're doing it elsewhere in your + // application + $eventManager = $e->getApplication()->getEventManager(); + $moduleRouteListener = new ModuleRouteListener(); + $moduleRouteListener->attach($eventManager); + } + + public function getServiceConfig() + { + return array( + 'factories' => array( + 'zfmodule_cache' => function ($sm) { + $config = $sm->get('Config'); + $storage = StorageFactory::factory($config['zfmodule']['cache']); + + return $storage; + }, + 'zfmodule_mapper_module' => function ($sm) { + $mapper = new Mapper\Module(); + $mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter')); + $mapper->setEntityPrototype(new Entity\Module); + $mapper->setHydrator(new Mapper\ModuleHydrator()); + return $mapper; + }, + 'zfmodule_service_module' => function ($sm) { + $service = new Service\Module; + return $service; + }, + 'zfmodule_service_repository' => function ($sm) { + $service = new Service\Repository; + $service->setApi($sm->get('EdpGithub\Client')); + return $service; + }, + /*'github_client' => function ($sm) { + $hybridAuth = $sm->get('HybridAuth'); + $adapter = $hybridAuth->getAdapter('github'); + $token = $adapter->getAccessToken(); + + $client = $sm->get('EdpGithubClient'); + $client->authenticate('url_token',$token['access_token'], null); + return $client; + }*/ + ), + ); + } +}