diff --git a/composer.json b/composer.json index 819be16f..b00fe92f 100644 --- a/composer.json +++ b/composer.json @@ -1,47 +1,47 @@ { - "name": "zendframework/modules.zendframework.com", - "description": "ZF2 Modules Site", - "license": "BSD-3-Clause", - "homepage": "https://zfmodules.com", - "require": { - "php": "~5.5", - "ext-intl": "*", - "doctrine/doctrine-module": "^0.9.0", - "doctrine/doctrine-orm-module": "^0.9.1", - "doctrine/migrations": "^1.1", - "evandotpro/edp-github": "0.2.*", - "ezyang/htmlpurifier": "4.6.*", - "monolog/monolog": "~1.12", - "phing/phing": "~2.10", - "socalnick/scn-social-auth": "1.14.1", - "zendframework/zendframework": "~2.3.0", - "zf-commons/zfc-user": "1.0.*", - "zfcampus/zf-development-mode": "~2.0" + "name": "zendframework/modules.zendframework.com", + "description": "ZF2 Modules Site", + "license": "BSD-3-Clause", + "homepage": "https://zfmodules.com", + "require": { + "php": "~5.5", + "ext-intl": "*", + "doctrine/doctrine-module": "^0.9.0", + "doctrine/doctrine-orm-module": "^0.9.1", + "doctrine/migrations": "^1.1", + "evandotpro/edp-github": "0.2.*", + "ezyang/htmlpurifier": "4.6.*", + "monolog/monolog": "~1.12", + "phing/phing": "~2.10", + "socalnick/scn-social-auth": "1.14.1", + "zendframework/zendframework": "~2.3.0", + "zf-commons/zfc-user": "1.0.*", + "zfcampus/zf-development-mode": "~2.0" + }, + "require-dev": { + "bjyoungblood/BjyProfiler": "1.1.0", + "fabpot/php-cs-fixer": "~1.4", + "fzaninotto/faker": "^1.4.0", + "phpunit/phpunit": "^4.8", + "zendframework/zend-developer-tools": "dev-master", + "zendframework/zftool": "dev-master" + }, + "autoload": { + "psr-4": { + "Application\\": "module/Application/src/Application", + "User\\": "module/User/src/User", + "ZfModule\\": "module/ZfModule/src/ZfModule" }, - "require-dev": { - "bjyoungblood/BjyProfiler": "1.1.0", - "fabpot/php-cs-fixer": "~1.4", - "fzaninotto/faker": "^1.4.0", - "phpunit/phpunit": "^4.8", - "zendframework/zend-developer-tools": "dev-master", - "zendframework/zftool": "dev-master" - }, - "autoload": { - "psr-4": { - "Application\\": "module/Application/src/Application", - "User\\": "module/User/src/User", - "ZfModule\\": "module/ZfModule/src/ZfModule" - }, - "classmap": [ - "vendor/hybridauth/hybridauth/hybridauth/Hybrid", - "vendor/hybridauth/hybridauth/additional-providers/hybridauth-github/Providers" - ] - }, - "autoload-dev": { - "psr-4": { - "ApplicationTest\\": "module/Application/test/ApplicationTest", - "UserTest\\": "module/User/test/UserTest", - "ZfModuleTest\\": "module/ZfModule/test/ZfModuleTest" - } + "classmap": [ + "vendor/hybridauth/hybridauth/hybridauth/Hybrid", + "vendor/hybridauth/hybridauth/additional-providers/hybridauth-github/Providers" + ] + }, + "autoload-dev": { + "psr-4": { + "ApplicationTest\\": "module/Application/test/ApplicationTest", + "UserTest\\": "module/User/test/UserTest", + "ZfModuleTest\\": "module/ZfModule/test/ZfModuleTest" } + } } diff --git a/module/Application/config/module.config.php b/module/Application/config/module.config.php index 32fb9141..52ca2a6e 100644 --- a/module/Application/config/module.config.php +++ b/module/Application/config/module.config.php @@ -1,5 +1,7 @@ View\Helper\SanitizeHtmlFactory::class, ], ], + 'doctrine' => [ + 'driver' => [ + 'application_entities' => [ + 'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', + 'cache' => 'array', + 'paths' => [__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'], + ], + 'orm_default' => [ + 'drivers' => [ + 'Application\Entity' => 'application_entities' + ] + ] + ], + ], ]; diff --git a/module/Application/src/Application/Controller/IndexController.php b/module/Application/src/Application/Controller/IndexController.php index 09ca9e56..5a76013c 100644 --- a/module/Application/src/Application/Controller/IndexController.php +++ b/module/Application/src/Application/Controller/IndexController.php @@ -4,25 +4,27 @@ use Zend\Feed\Writer\Feed; use Zend\Mvc\Controller\AbstractActionController; +use Zend\Paginator; use Zend\View\Model\FeedModel; use Zend\View\Model\ViewModel; -use ZfModule\Mapper; +use ZfModule\Mapper\ModuleToFeed; +use ZfModule\Service; class IndexController extends AbstractActionController { const MODULES_PER_PAGE = 15; /** - * @var Mapper\Module + * @var Service\Module */ - private $moduleMapper; + private $moduleService; /** - * @param Mapper\Module $moduleMapper + * @param Service\Module $moduleService */ - public function __construct(Mapper\Module $moduleMapper) + public function __construct(Service\Module $moduleService) { - $this->moduleMapper = $moduleMapper; + $this->moduleService = $moduleService; } /** @@ -33,7 +35,7 @@ public function indexAction() $query = $this->params()->fromQuery('query', null); $page = (int) $this->params()->fromQuery('page', 1); - $repositories = $this->moduleMapper->pagination($page, self::MODULES_PER_PAGE, $query, 'created_at', 'DESC'); + $repositories = $this->moduleService->findModules($query, ['m.created_at DESC'], $page, self::MODULES_PER_PAGE); return new ViewModel([ 'repositories' => $repositories, @@ -57,10 +59,10 @@ public function feedAction() // Get the recent modules $page = 1; - $modules = $this->moduleMapper->pagination($page, self::MODULES_PER_PAGE, null, 'created_at', 'DESC'); + $modules = $this->moduleService->findModules(null, ['m.created_at DESC'], $page, self::MODULES_PER_PAGE); // Load them into the feed - $mapper = new Mapper\ModuleToFeed($feed, $url); + $mapper = new ModuleToFeed($feed, $url); $mapper->addModules($modules); // Render the feed diff --git a/module/Application/src/Application/Controller/IndexControllerFactory.php b/module/Application/src/Application/Controller/IndexControllerFactory.php index 954cdabc..de1e96ed 100644 --- a/module/Application/src/Application/Controller/IndexControllerFactory.php +++ b/module/Application/src/Application/Controller/IndexControllerFactory.php @@ -5,7 +5,7 @@ use Zend\Mvc\Controller\ControllerManager; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; -use ZfModule\Mapper; +use ZfModule\Service; class IndexControllerFactory implements FactoryInterface { @@ -19,9 +19,9 @@ public function createService(ServiceLocatorInterface $controllerManager) /* @var ControllerManager $controllerManager */ $serviceManager = $controllerManager->getServiceLocator(); - /* @var Mapper\Module $moduleMapper */ - $moduleMapper = $serviceManager->get(Mapper\Module::class); + /* @var Service\Module $moduleService */ + $moduleService = $serviceManager->get(Service\Module::class); - return new IndexController($moduleMapper); + return new IndexController($moduleService); } } diff --git a/module/Application/src/Application/Controller/SearchController.php b/module/Application/src/Application/Controller/SearchController.php index 927d4105..2f6d22bc 100644 --- a/module/Application/src/Application/Controller/SearchController.php +++ b/module/Application/src/Application/Controller/SearchController.php @@ -4,28 +4,28 @@ use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; -use ZfModule\Mapper; +use ZfModule\Service; class SearchController extends AbstractActionController { /** - * @var Mapper\Module + * @var Service\Module */ - private $moduleMapper; + private $moduleService; /** - * @param Mapper\Module $moduleMapper + * @param Service\Module $moduleService */ - public function __construct(Mapper\Module $moduleMapper) + public function __construct(Service\Module $moduleService) { - $this->moduleMapper = $moduleMapper; + $this->moduleService = $moduleService; } public function indexAction() { $query = $this->params()->fromQuery('query', null); - $results = $this->moduleMapper->findByLike($query); + $results = $this->moduleService->findModules($query, ['m.name']); $viewModel = new ViewModel([ 'results' => $results, diff --git a/module/Application/src/Application/Controller/SearchControllerFactory.php b/module/Application/src/Application/Controller/SearchControllerFactory.php index d8a5be64..00f58608 100644 --- a/module/Application/src/Application/Controller/SearchControllerFactory.php +++ b/module/Application/src/Application/Controller/SearchControllerFactory.php @@ -5,7 +5,7 @@ use Zend\Mvc\Controller\ControllerManager; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; -use ZfModule\Mapper; +use ZfModule\Service; class SearchControllerFactory implements FactoryInterface { @@ -19,9 +19,9 @@ public function createService(ServiceLocatorInterface $controllerManager) /* @var ControllerManager $controllerManager */ $serviceManager = $controllerManager->getServiceLocator(); - /* @var Mapper\Module $moduleMapper */ - $moduleMapper = $serviceManager->get(Mapper\Module::class); + /* @var Service\Module $moduleService */ + $moduleService = $serviceManager->get(Service\Module::class); - return new SearchController($moduleMapper); + return new SearchController($moduleService); } } diff --git a/module/Application/view/application/index/index.phtml b/module/Application/view/application/index/index.phtml index 68f6f54b..083c87a8 100644 --- a/module/Application/view/application/index/index.phtml +++ b/module/Application/view/application/index/index.phtml @@ -29,7 +29,7 @@ escapeHtml($module->getIdentifier()); ?>

-

Created: dateFormat($module->getCreatedAtDateTime(), IntlDateFormatter::SHORT, IntlDateFormatter::SHORT); ?>

+

Created: dateFormat($module->getCreatedAt(), IntlDateFormatter::SHORT, IntlDateFormatter::SHORT); ?>

Show module on GitHub @@ -55,6 +55,6 @@
- newUsers(); ?> + newUsers(); ?> diff --git a/module/User/config/module.config.php b/module/User/config/module.config.php index 857f1fc0..15ba7d64 100644 --- a/module/User/config/module.config.php +++ b/module/User/config/module.config.php @@ -1,5 +1,7 @@ GitHub\LoginListener::class, ], ], + 'doctrine' => [ + 'driver' => [ + 'user_entities' => [ + 'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', + 'cache' => 'array', + 'paths' => [__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'], + ], + 'orm_default' => [ + 'drivers' => [ + 'User\Entity' => 'user_entities' + ] + ] + ], + ], ]; diff --git a/module/ZfModule/config/module.config.php b/module/ZfModule/config/module.config.php index 1640c643..4a80d452 100644 --- a/module/ZfModule/config/module.config.php +++ b/module/ZfModule/config/module.config.php @@ -1,5 +1,7 @@ [ + 'driver' => [ + 'zfmodule_entities' => [ + 'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', + 'cache' => 'array', + 'paths' => [__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'], + ], + 'orm_default' => [ + 'drivers' => [ + 'ZfModule\Entity' => 'zfmodule_entities' + ] + ] + ], + ], ]; diff --git a/module/ZfModule/src/ZfModule/Entity/Module.php b/module/ZfModule/src/ZfModule/Entity/Module.php index 75da748c..bd37ad73 100644 --- a/module/ZfModule/src/ZfModule/Entity/Module.php +++ b/module/ZfModule/src/ZfModule/Entity/Module.php @@ -1,65 +1,104 @@ getOwner(); + $name = $this->getName(); + + if (empty($owner) || empty($name)) { + return; + } + + return sprintf('%s/%s', $owner, $name); + } /** * @return string */ - public function getPhotoUrl() + public function getUrl() { - return $this->photoUrl; + return $this->url; } /** - * @param string $photoUrl + * @param string $url */ - public function setPhotoUrl($photoUrl) + public function setUrl($url) { - $this->photoUrl = $photoUrl; + $this->url = $url; } /** @@ -79,121 +118,98 @@ public function setOwner($owner) } /** - * @return string + * @return int */ - public function getUpdatedAt() + public function getId() { - return $this->updatedAt; + return $this->id; } /** - * @param string $updatedAt + * @param int $id */ - public function setUpdatedAt($updatedAt) + public function setId($id) { - $this->updatedAt = $updatedAt; + $this->id = $id; } /** * @return string */ - public function getCreatedAt() - { - return $this->createdAt; - } - - /** - * @return DateTime - */ - public function getCreatedAtDateTime() + public function getName() { - return new DateTime($this->getCreatedAt()); + return $this->name; } /** - * @param string $createdAt + * @param string $name */ - public function setCreatedAt($createdAt) + public function setName($name) { - $this->createdAt = $createdAt; + $this->name = $name; } /** - * @return int + * @return string */ - public function getId() + public function getDescription() { - return $this->id; + return $this->description; } /** - * @param int $id + * @param string $description */ - public function setId($id) + public function setDescription($description) { - $this->id = (int) $id; + $this->description = $description; } /** - * @return string + * @return DateTime */ - public function getUrl() + public function getCreatedAt() { - return $this->url; + return $this->created_at; } /** - * @param string $url + * @param DateTime $created_at */ - public function setUrl($url) + public function setCreatedAt($created_at) { - $this->url = $url; + $this->created_at = $created_at; } /** - * @return string + * @return DateTime */ - public function getDescription() + public function getUpdatedAt() { - return $this->description; + return $this->updated_at; } /** - * @param string $description + * @param DateTime $updated_at */ - public function setDescription($description) + public function setUpdatedAt($updated_at) { - $this->description = $description; + $this->updated_at = $updated_at; } /** * @return string */ - public function getName() + public function getPhotoUrl() { - return $this->name; + return $this->photo_url; } /** - * @param string $name + * @param string $photo_url */ - public function setName($name) + public function setPhotoUrl($photo_url) { - $this->name = $name; - } - - /** - * @return string|null - */ - public function getIdentifier() - { - $owner = $this->getOwner(); - $name = $this->getName(); - - if (empty($owner) || empty($name)) { - return; - } - - return sprintf('%s/%s', $owner, $name); + $this->photo_url = $photo_url; } } diff --git a/module/ZfModule/src/ZfModule/Mapper/ModuleToFeed.php b/module/ZfModule/src/ZfModule/Mapper/ModuleToFeed.php index 741fda4e..b77c0075 100644 --- a/module/ZfModule/src/ZfModule/Mapper/ModuleToFeed.php +++ b/module/ZfModule/src/ZfModule/Mapper/ModuleToFeed.php @@ -65,7 +65,7 @@ public function addModule(ModuleEntity $module) $entry->setDescription($moduleDescription); $entry->setLink($this->urlPlugin->fromRoute('view-module', $urlParams, ['force_canonical' => true])); $entry->addAuthor(['name' => $module->getOwner()]); - $entry->setDateCreated($module->getCreatedAtDateTime()); + $entry->setDateCreated($module->getCreatedAt()); $this->feed->addEntry($entry); diff --git a/module/ZfModule/src/ZfModule/Repository/Module.php b/module/ZfModule/src/ZfModule/Repository/Module.php new file mode 100644 index 00000000..56a08b5f --- /dev/null +++ b/module/ZfModule/src/ZfModule/Repository/Module.php @@ -0,0 +1,45 @@ +findAll($limit); + } + + $dqlQuery = 'SELECT m FROM ZfModule\Entity\Module m WHERE m.name LIKE :term'; + + if ($orderBy && count($orderBy) >= 1) { + $dqlQuery .= ' ORDER BY ' . implode(',', $orderBy); + } + + $query = $this->getEntityManager()->createQuery($dqlQuery); + $query->setParameter('term', '%' . $term . '%'); + $query->setMaxResults($limit); + + return $query; + } + + public function findAll($limit = null, $offset = null) + { + $query = $this->getEntityManager()->createQuery('SELECT m FROM ZfModule\Entity\Module m'); + //$query->setFirstResult($offset); + //$query->setMaxResults($limit); + + return $query; + } + + public function countTotalModules() + { + $query = $this->getEntityManager()->createQuery('SELECT count(m) FROM ZfModule\Entity\Module m'); + return $query; + } + +} diff --git a/module/ZfModule/src/ZfModule/Service/Module.php b/module/ZfModule/src/ZfModule/Service/Module.php index de66bad9..98255f99 100644 --- a/module/ZfModule/src/ZfModule/Service/Module.php +++ b/module/ZfModule/src/ZfModule/Service/Module.php @@ -2,21 +2,24 @@ namespace ZfModule\Service; +use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator; +use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator; use EdpGithub\Client; use EdpGithub\Collection\RepositoryCollection; use EdpGithub\Http\Client as HttpClient; use stdClass; use Zend\Http; +use Zend\Paginator; use ZfcBase\EventManager\EventProvider; use ZfModule\Entity; -use ZfModule\Mapper; +use ZfModule\Repository; class Module extends EventProvider { /** - * @var Mapper\Module + * @var Repository\Module */ - private $moduleMapper; + private $moduleRepository; /** * @var Client @@ -24,15 +27,61 @@ class Module extends EventProvider private $githubClient; /** - * @param Mapper\Module $moduleMapper + * @param Repository\Module $moduleRepository * @param Client $githubClient */ - public function __construct(Mapper\Module $moduleMapper, Client $githubClient) + public function __construct(Repository\Module $moduleRepository, Client $githubClient) { - $this->moduleMapper = $moduleMapper; + $this->moduleRepository = $moduleRepository; $this->githubClient = $githubClient; } + /** + * Return Total Modules + * @return int + */ + public function getTotalModuleCount() + { + return $this->moduleRepository->countTotalModules()->getSingleScalarResult(); + } + + /** + * Get all Modules + * + * @param null $limit + * @param null $offset + * @return array + */ + public function getModules($limit = null, $offset = null) + { + return $this->moduleRepository->findAll($limit, $offset)->getResult(); + } + + /** + * Find Modules + * + * @param $searchTerm + * @param $orderBy + * @param int $currentPage + * @param null $perPage + * @return array|Paginator\Paginator + */ + public function findModules($searchTerm, $orderBy, $currentPage = 1, $perPage = null) + { + $modules = $this->moduleRepository->findModulesByName($searchTerm, $orderBy); + + if ($perPage) { + $paginatorAdapter = new DoctrinePaginator(new ORMPaginator($modules)); + $paginator = new Paginator\Paginator($paginatorAdapter); + $paginator->setItemCountPerPage($perPage); + $paginator->setCurrentPageNumber($currentPage); + + return $paginator; + } + + return $modules->getResult(); + } + /** * @param stdClass $repository * @return Entity\Module diff --git a/module/ZfModule/src/ZfModule/Service/ModuleFactory.php b/module/ZfModule/src/ZfModule/Service/ModuleFactory.php index c22f5502..b6c34622 100644 --- a/module/ZfModule/src/ZfModule/Service/ModuleFactory.php +++ b/module/ZfModule/src/ZfModule/Service/ModuleFactory.php @@ -2,10 +2,11 @@ namespace ZfModule\Service; +use Doctrine\ORM\EntityManager; use EdpGithub\Client; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; -use ZfModule\Mapper; +use ZfModule\Entity; class ModuleFactory implements FactoryInterface { @@ -16,12 +17,15 @@ class ModuleFactory implements FactoryInterface */ public function createService(ServiceLocatorInterface $serviceLocator) { - /* @var Mapper\Module $moduleMapper */ - $moduleMapper = $serviceLocator->get(Mapper\Module::class); + /* @var EntityManager $entityManager */ + $entityManager = $serviceLocator->get(EntityManager::class); + + /* @var Entity\Module $moduleRepository */ + $moduleRepository = $entityManager->getRepository(Entity\Module::class); /* @var Client $githubClient */ $githubClient = $serviceLocator->get('EdpGithub\Client'); - return new Module($moduleMapper, $githubClient); + return new Module($moduleRepository, $githubClient); } } diff --git a/module/ZfModule/src/ZfModule/View/Helper/TotalModules.php b/module/ZfModule/src/ZfModule/View/Helper/TotalModules.php index b38cdb25..1174825b 100644 --- a/module/ZfModule/src/ZfModule/View/Helper/TotalModules.php +++ b/module/ZfModule/src/ZfModule/View/Helper/TotalModules.php @@ -3,14 +3,14 @@ namespace ZfModule\View\Helper; use Zend\View\Helper\AbstractHelper; -use ZfModule\Mapper; +use ZfModule\Service; class TotalModules extends AbstractHelper { /** - * @var Mapper\Module + * @var Service\Module */ - private $moduleMapper; + private $moduleService; /** * @var int @@ -18,11 +18,11 @@ class TotalModules extends AbstractHelper private $total; /** - * @param Mapper\Module $moduleMapper + * @param Service\Module $moduleService */ - public function __construct(Mapper\Module $moduleMapper) + public function __construct(Service\Module $moduleService) { - $this->moduleMapper = $moduleMapper; + $this->moduleService = $moduleService; } /** @@ -31,7 +31,7 @@ public function __construct(Mapper\Module $moduleMapper) public function __invoke() { if ($this->total === null) { - $this->total = $this->moduleMapper->getTotal(); + $this->total = $this->moduleService->getTotalModuleCount(); } return $this->total; diff --git a/module/ZfModule/src/ZfModule/View/Helper/TotalModulesFactory.php b/module/ZfModule/src/ZfModule/View/Helper/TotalModulesFactory.php index 791025e8..7c924017 100644 --- a/module/ZfModule/src/ZfModule/View/Helper/TotalModulesFactory.php +++ b/module/ZfModule/src/ZfModule/View/Helper/TotalModulesFactory.php @@ -2,10 +2,11 @@ namespace ZfModule\View\Helper; +use Doctrine\ORM\EntityManager; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use Zend\View\HelperPluginManager; -use ZfModule\Mapper; +use ZfModule\Service; class TotalModulesFactory implements FactoryInterface { @@ -17,11 +18,11 @@ class TotalModulesFactory implements FactoryInterface public function createService(ServiceLocatorInterface $helperPluginManager) { /* @var HelperPluginManager $helperPluginManager */ - $serviceManager = $helperPluginManager->getServiceLocator(); + $serviceLocator = $helperPluginManager->getServiceLocator(); - /* @var Mapper\Module $moduleMapper */ - $moduleMapper = $serviceManager->get(Mapper\Module::class); + /* @var Module $moduleService */ + $moduleService = $serviceLocator->get(Service\Module::class); - return new TotalModules($moduleMapper); + return new TotalModules($moduleService); } }