Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

Commit

Permalink
Enhancement: Allow to load module classes with PSR-0 autoloader
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Nov 20, 2014
1 parent 43643ab commit b55c862
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 176 deletions.
38 changes: 2 additions & 36 deletions module/Application/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
44 changes: 44 additions & 0 deletions module/Application/src/Application/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/ZendSkeletonApplication 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__,
),
),
);
}


}
73 changes: 9 additions & 64 deletions module/User/Module.php
Original file line number Diff line number Diff line change
@@ -1,65 +1,10 @@
<?php

namespace User;

use ZfcBase\Module\AbstractModule;
use Zend\ModuleManager\ModuleManager;
use Zend\Mvc\ApplicationInterface;

class Module extends AbstractModule
{
public function bootstrap(ModuleManager $moduleManager, ApplicationInterface $app)
{
$em = $app->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';
65 changes: 65 additions & 0 deletions module/User/src/User/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace User;

use ZfcBase\Module\AbstractModule;
use Zend\ModuleManager\ModuleManager;
use Zend\Mvc\ApplicationInterface;

class Module extends AbstractModule
{
public function bootstrap(ModuleManager $moduleManager, ApplicationInterface $app)
{
$em = $app->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__;
}
}
77 changes: 1 addition & 76 deletions module/ZfModule/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
84 changes: 84 additions & 0 deletions module/ZfModule/src/ZfModule/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* 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
*/

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(
__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;
}*/
),
);
}
}

0 comments on commit b55c862

Please sign in to comment.