Advertisement
ssmusoke

ZF Bootstrap

Mar 3rd, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.58 KB | None | 0 0
  1. <?php
  2. require_once APPLICATION_PATH . '/../library/Doctrine/Common/ClassLoader.php';
  3.  
  4. use Doctrine\Common\ClassLoader,
  5.     Doctrine\ORM\Configuration as DoctrineConfiguration,
  6.     Doctrine\Common\EventManager as DoctrineEventManager,
  7.     Doctrine\ORM\EntityManager as DoctrineEntityManager,
  8.     Doctrine\Common\Cache\ApcCache as DoctrineApcCache,
  9.     Doctrine\Common\Cache\ArrayCache as DoctrineArrayCache;
  10.  
  11. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
  12.    
  13.     public function _initClassLoaders() {
  14.         debugMessage(get_include_path());
  15.         $loader = new ClassLoader('Doctrine\ORM');
  16.         $loader->register();
  17.         $loader = new ClassLoader('Doctrine\Common');
  18.         $loader->register();
  19.         $loader = new ClassLoader('Doctrine\DBAL');
  20.         $loader->register();
  21.         $loader = new ClassLoader('Symfony');
  22.         $loader->register();
  23.     }
  24.    
  25.     public function _initDoctrineEntityManager() {
  26.         $this->bootstrap(array('classLoaders', 'doctrineCache'));
  27.         $zendConfig = $this->getOptions();
  28.        
  29.         // parameters required for connecting to the database.
  30.         // the required attributes are driver, host, user, password and dbname
  31.         $connectionParameters = $zendConfig['resources']['db']['params'];
  32.        
  33.         // Doctrine 2 expects a driver attribute instead of adapter
  34.         $connectionParameters['driver'] = $zendConfig['resources']['db']['adapter'];
  35.         // Doctrine 2 expects a user variable instead of username so change it here
  36.         $connectionParameters['user'] = $connectionParameters['username'];
  37.  
  38.         // now initialize the configuration object
  39.         $configuration = new DoctrineConfiguration();
  40.         // the metadata cache is used to avoid parsing all mapping information every time
  41.         // the framework is initialized.
  42.         $configuration->setMetadataCacheImpl($this->getResource('doctrineCache'));
  43.         // for performance reasons, it is also recommended to use a result cache
  44.         $configuration->setResultCacheImpl($this->getResource('doctrineCache'));
  45.  
  46.         // if you set this option to true, Doctrine 2 will generate proxy classes for your entities
  47.         // on the fly. This has of course impact on the performance and should therefore be disabled
  48.         // in the production environment
  49.         $configuration->setAutoGenerateProxyClasses($zendConfig['doctrine']['autoGenerateProxyClasses']);
  50.  
  51.         // the directory, where your proxy classes live
  52.         $configuration->setProxyDir($zendConfig['doctrine']['proxyPath']);
  53.         // the proxy classes' namespace
  54.         $configuration->setProxyNamespace($zendConfig['doctrine']['proxyNamespace']);
  55.  
  56.         // the next option tells doctrine which description language we want to use for the mapping
  57.         // information
  58.         $configuration->setMetadataDriverImpl(
  59.             $configuration->newDefaultAnnotationDriver(
  60.                  $zendConfig['doctrine']['entityPath']));
  61.  
  62.         // next, we create an event manager
  63.         $eventManager = new DoctrineEventManager();
  64.  
  65.         // now we have everything required to initialize the entity manager
  66.         $entityManager = DoctrineEntityManager::create($connectionParameters, $configuration, $eventManager);
  67.        
  68.         Zend_Registry::set('em', $entityManager);
  69.        
  70.         $cmf = $entityManager->getMetadataFactory();
  71.         $class = $cmf->getMetadataFor('AclGroup');
  72.         debugMessage($class);
  73.  
  74.         return $entityManager;
  75.     }
  76.  
  77.     /**
  78.      * @return Doctrine\Common\CacheProvider
  79.      */
  80.     public function _initDoctrineCache() {
  81.         $cache = null;
  82.         if (APPLICATION_ENV === 'development') {
  83.             $cache = new DoctrineArrayCache();
  84.         } else {
  85.             $cache = new DoctrineArrayCache(); // could be changed depending on what the hosting provider supports
  86.         }
  87.         return $cache;
  88.     }
  89.     /**
  90.      * Intialize the memory usage logging
  91.      *
  92.     public function _initEnableMemoryUsageLogging() {
  93.         $writer = new Zend_Log_Writer_Stream ( APPLICATION_PATH. '/logs/memory_usage.log' );
  94.         $log = new Zend_Log ( $writer );
  95.         $plugin = new MemoryPeakUsageLog ( $log );
  96.         // Use a high stack index to delay execution until other        
  97.         // plugins are finished, and their memory can also be accounted for.        
  98.         Zend_Controller_Front::getInstance()->registerPlugin ($plugin, 1000 );
  99.     }*/
  100.    
  101.     /**
  102.      * Initialize the email template
  103.      */
  104.     public function _initMail() {
  105.         # Zend Mail instance
  106.         // create a new instance
  107.         $mailer = new Zend_Mail('utf8');
  108.         // set the default transport configured in application.ini
  109.         $mailer->setDefaultTransport($this->getPluginResource('mail')->getMail());
  110.        
  111.         // set the default to and from addresses
  112.         $mail_config = new Zend_Config($this->getPluginResource('mail')->getOptions());
  113.         $mailer->setDefaultFrom($mail_config->defaultFrom->email, $mail_config->defaultFrom->name);
  114.         $mailer->setDefaultReplyTo($mail_config->defaultReplyTo->email, $mail_config->defaultReplyTo->name);
  115.        
  116.         // add the mail instance to the registry
  117.         Zend_Registry::set("mail", $mailer);
  118.     }
  119.     /**
  120.      * Initialize the logger, cache and translate instances
  121.      *
  122.      */
  123.     public function _initEnv() {
  124.         Zend_Registry::set("log", $this->getPluginResource('log')->getLog());
  125.         Zend_Registry::set("translate", $this->getPluginResource('translate')->getTranslate());
  126.         Zend_Registry::set("cache", $this->getPluginResource('cachemanager')->getCacheManager()->getCache('cache'));
  127.        
  128.         // the database adapter for the session
  129.         Zend_Registry::set("dbAdapter", $this->getPluginResource('db')->getDbAdapter());
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement