所以 - 我想将我的代码切换到依赖注入、依赖注入容器 (DIC) 范式。而且我一直在阅读它……在我看来,我一直在这样做……也许……
我在做什么 - 我正在为每个单独的项目创建命名空间类(容器?),我通常称之为 Api,然后我将实例化并从中获取配置的对象。实际示例:
<?php
/**
* @namespace
*/
namespace SomeNamespace;
/**
* api
*/
class api {
public function __construct() {
/*
* Requiring all of the common files.
*/
require_once( VENDORS_PATH . DIRECTORY_SEPARATOR . 'PHPMailer' . DIRECTORY_SEPARATOR . 'class.phpmailer.php' );
require_once( VENDORS_PATH . DIRECTORY_SEPARATOR . 'PHPMailer' . DIRECTORY_SEPARATOR . 'class.smtp.php' );
}
private function getPostgresqlPreprocessor() {
$spp = new \SomeNamespace\Utils\PostgresSearchPreProcessor();
$settingsService = $this->getSettingsService();
$settings = $settingsService->readSettings();
$spp->setValidator( new \Auro\Validation\Validator() )
->setSettings( $settings )
->setConfig( require( CONFIGS_PATH . DIRECTORY_SEPARATOR . 'models_craig_config.php' ) );
return $spp;
}
public function getDalApi() {
return new \DalServices\Api();
}
public function getFeedsService() {
$fc = \Auro\Mvc\Front::getInstance();
$feedsService = new \SomeNamespace\Feeds();
$renderer = new \Auro\View\Renderer();
$renderer->setBasePath( VIEWS_PATH . DIRECTORY_SEPARATOR . 'feeds' );
$feedsService
->setDalApi( new \DalServices\Api() )
->setSomeNamespaceApi( $this )
->setResponse( $fc->getResponse() )
->setPaginator( new \Auro\View\Paginator() )
->setConfig( require( CONFIGS_PATH . DIRECTORY_SEPARATOR . 'models_feeds_config.php' ) )
->setCams( require( CONFIGS_PATH . DIRECTORY_SEPARATOR . 'webcams.php' ) )
->setRenderer( $renderer );
return $feedsService;
}
public function getSettingsService() {
$fc = \Auro\Mvc\Front::getInstance();
$settingsService = new \SomeNamespace\Settings;
$renderer = new \Auro\View\Renderer();
$renderer->setBasePath( VIEWS_PATH . DIRECTORY_SEPARATOR . 'settings' );
$settingsService
->setSomeNamespaceApi( $this )
->setValidator( new \Auro\Validation\Validator() )
->setConfig( require( CONFIGS_PATH . DIRECTORY_SEPARATOR . 'models_craig_config.php' ) )
->setDalApi( new \DalServices\Api() )
->setResponse( $fc->getResponse() )
->setRenderer( $renderer );
return $settingsService;
}
public function getUsersService() {
$fc = \Auro\Mvc\Front::getInstance();
$usersService = new \SomeNamespace\Users();
$renderer = new \Auro\View\Renderer();
$renderer->setBasePath( VIEWS_PATH . DIRECTORY_SEPARATOR . 'users' );
$usersService
->setSomeNamespaceApi( $this )
->setValidator( new \Auro\Validation\Validator() )
->setConfig( require( CONFIGS_PATH . DIRECTORY_SEPARATOR . 'models_craig_config.php' ) )
->setAvailableCurrencies( require( CONFIGS_PATH . DIRECTORY_SEPARATOR . 'currencies.php' ) )
->setLanguages( require( CONFIGS_PATH . DIRECTORY_SEPARATOR . 'languages.php' ) )
->setPostgersqlPreprocessor( $this->getPostgresqlPreprocessor() )
->setResponse( $fc->getResponse() )
->setDalApi( new \DalServices\Api() )
->setRenderer( $renderer );
return $usersService;
}
public function getVrcommentsService() {
$fc = \Auro\Mvc\Front::getInstance();
$vrcommentsService = new \SomeNamespace\Vrcomments();
$renderer = new \Auro\View\Renderer();
$renderer->setBasePath( VIEWS_PATH . DIRECTORY_SEPARATOR . 'vrcomments' );
$vrcommentsService
->setSomeNamespaceApi( $this )
->setValidator( new \Auro\Validation\Validator() )
->setConfig( require( CONFIGS_PATH . DIRECTORY_SEPARATOR . 'models_craig_config.php' ) )
->setPostgersqlPreprocessor( $this->getPostgresqlPreprocessor() )
->setPaginator( new \Auro\View\Paginator() )
->setResponse( $fc->getResponse() )
->setDalApi( new \DalServices\Api() )
->setRenderer( $renderer );
return $vrcommentsService;
}
public function getVrentalsService() {
$fc = \Auro\Mvc\Front::getInstance();
$renderer = new \Auro\View\Renderer();
$renderer->setBasePath( VIEWS_PATH . DIRECTORY_SEPARATOR . 'vrentals' );
$vrentalsService = new \SomeNamespace\Vrentals();
$vrentalsService
->setSomeNamespaceApi( $this )
->setValidator( new \Auro\Validation\Validator() )
->setConfig( require( CONFIGS_PATH . DIRECTORY_SEPARATOR . 'models_craig_config.php' ) )
->setPostgersqlPreprocessor( $this->getPostgresqlPreprocessor() )
->setPaginator( new \Auro\View\Paginator() )
->setDalApi( new \DalServices\Api() )
->setRenderer( $renderer )
->setResponse( $fc->getResponse() )
->setMailConfig( require( CONFIGS_PATH . DIRECTORY_SEPARATOR . 'models_craig_mail.php' ) )
->setMailer( new \PHPMailer( true ) );
return $vrentalsService;
}
public function getNoAikService() {
$fc = \Auro\Mvc\Front::getInstance();
$renderer = new \Auro\View\Renderer();
$renderer->setBasePath( VIEWS_PATH . DIRECTORY_SEPARATOR . 'noaik' );
$AikService = new \SomeNamespace\Noaik();
$noAikService
->setSomeNamespaceApi( $this )
->setValidator( new \Auro\Validation\Validator() )
->setConfig( require( CONFIGS_PATH . DIRECTORY_SEPARATOR . 'models_craig_config.php' ) )
->setPostgersqlPreprocessor( $this->getPostgresqlPreprocessor() )
->setPaginator( new \Auro\View\Paginator() )
->setResponse( $fc->getResponse() )
->setDalApi( new \DalServices\Api() )
->setRenderer( $renderer )
->setMailConfig( require( CONFIGS_PATH . DIRECTORY_SEPARATOR . 'models_craig_mail.php' ) )
->setMailer( new \PHPMailer( true ) );
return $noAikService;
}
public function getTotalauthService() {
$fc = \Auro\Mvc\Front::getInstance();
$renderer = new \Auro\View\Renderer();
$renderer->setBasePath( VIEWS_PATH . DIRECTORY_SEPARATOR . 'totalauth' );
$totalauthService = new \SomeNamespace\Totalauth();
$totalauthService
->setSomeNamespaceApi( $this )
->setValidator( new \Auro\Validation\Validator() )
->setRenderer( $renderer )
->setResponse( $fc->getResponse() )
->setDalApi( new \DalServices\Api() )
->setConfig( require( CONFIGS_PATH . DIRECTORY_SEPARATOR . 'models_totalauth_config.php' ) )
->setMailConfig( require( CONFIGS_PATH . DIRECTORY_SEPARATOR . 'models_totalauth_mail.php' ) )
->setMailer( new \PHPMailer( true ) );
return $totalauthService;
}
public function getQuickmailService() {
$fc = \Auro\Mvc\Front::getInstance();
$quickmailService = new \SomeNamespace\Quickmail();
$quickmailService
->setSomeNamespaceApi( $this )
->setValidator( new \Auro\Validation\Validator() )
->setResponse( $fc->getResponse() )
->setMailConfig( require( CONFIGS_PATH . DIRECTORY_SEPARATOR . 'models_quickmail_mail.php' ) )
->setMailer( new \PHPMailer( true ) );
return $quickmailService;
}
}
?>
有人可以确认这实际上是依赖注入容器,还有什么可以改进的?