Skip to content

Instantly share code, notes, and snippets.

@omerucel
Created July 19, 2011 17:27
Show Gist options
  • Save omerucel/1093163 to your computer and use it in GitHub Desktop.
Save omerucel/1093163 to your computer and use it in GitHub Desktop.
Zend Framework ile gelişmiş modül sistemi
<?php
/**
Bağlantılar:
http://url/ -> application/controllers/IndexController.php
http://url/api/ -> application/api/controllers/IndexController.php
http://url/api/v1/ -> application/api/v1/controllers/IndexController.php
http://url/api/v1/user/ -> application/api/v1/user/controllers/IndexController.php
http://url/api/v2/ -> application/api/v2/controllers/IndexController.php
Dizin yapısı
application
-- configs
---- application.ini
-- controllers
---- ErrorController.php -> class ErrorController
---- IndexController.php -> class IndexController
-- modules
---- api
------ controllers
-------- ErrorController.php -> class Api_ErrorController
-------- IndexController.php -> class Api_IndexController
------ v1
-------- controllers
---------- ErrorController.php -> class Api_V1_ErrorController
---------- IndexController.php -> class Api_V1_IndexController
-------- user
---------- controllers
------------ ErrorController.php -> class Api_V1_User_ErrorController
------------ IndexController.php -> class Api_V1_User_IndexController
------ v2
-------- controllers
---------- ErrorController.php -> class Api_V2_ErrorController
---------- IndexController.php -> class Api_V2_IndexController
-- Bootstrap.php
library
-- Ou
---- Application
------ Resource
-------- Modularity.php
Örnek Ayar dosyası..
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
autoloaderNamespaces[] = 'Ou_'
; setting Modularity plugin path
pluginPaths.Ou_Application_Resource = 'Ou/Application/Resource'
; Modularity settings
resources.modularity.api.basePath = APPLICATION_PATH "/modules/api"
resources.modularity.site.basePath = APPLICATION_PATH "/modules/site"
resources.modularity.admin.basePath = APPLICATION_PATH "/modules/admin"
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
*/
class Ou_Application_Resource_Modularity extends Zend_Application_Resource_ResourceAbstract
{
public function init()
{
$options = $this->getOptions();
$front = Zend_Controller_Front::getInstance();
foreach($options as $key => $value)
{
$moduleName = $key;
$basePath = $value['basePath'];
// modül dizini kontrol ediliyor.
if (!is_dir($basePath))
continue;
$this->_setup($basePath);
}
return $this;
}
protected function _setup($basePath)
{
$front = Zend_Controller_Front::getInstance();
$this->_registerModule($basePath);
$dirIterator = new DirectoryIterator($basePath);
foreach($dirIterator as $fileInfo)
{
if (!$fileInfo->isDir() || $fileInfo->isDot())
continue;
// Zend tarafından tanınan klasörleri kullanma.
switch($fileInfo->getBasename())
{
case $front->getModuleControllerDirectoryName():
case 'views':
case 'services':
case 'plugins':
case 'models':
case 'forms':
break;
default:
$this->_registerModule($fileInfo->getPathname());
$this->_setup($fileInfo->getPathname());
break;
}
}
}
protected function _registerModule($basePath)
{
$front = Zend_Controller_Front::getInstance();
// controller dizini kontrol ediliyor.
$front->addModuleDirectory($basePath);
$controllerDir = realpath($basePath.'/'.$front->getModuleControllerDirectoryName());
if (!is_dir($controllerDir))
return false;
// url ayarlanıyor.
// todo : modules için bir çözüm bulunmalı.
$_prefix = str_replace(APPLICATION_PATH.'/modules/', '', $basePath);
$_prefix = ucwords(str_replace('/', ' ', $_prefix));
$classNamespace = str_replace(' ', '_', $_prefix);
$_prefix = explode(' ', $_prefix);
$urlPath = array();
foreach($_prefix as $_section)
{
$urlPath[]= $this->_formatUrlString($_section);
}
$urlPath = implode('/', $urlPath);
$front->addControllerDirectory($controllerDir, $classNamespace);
$route = new Zend_Controller_Router_Route($urlPath.'/:controller/:action/*',array(
'module' => $classNamespace,
'controller' => false,
'action' => false
));
$front->getRouter()->addRoute($urlPath, $route);
$resourceLoader = new Zend_Application_Module_Autoloader(array(
'basePath' => $basePath,
'namespace' => $classNamespace
));
}
/**
* @see http://www.simonrjones.net/code/sub-modules/Submodules.phps
*/
protected function _formatUrlString ($string)
{
$string = strtolower(substr($string, 0, 1)) . substr($string, 1, strlen($string) - 1);
$string = preg_replace('/([A-Z]{1})/', '-$1', $string);
$string = strtolower($string);
$string = preg_replace('/[^a-z0-9\-]/', '', $string);
return $string;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment