| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ApplicationTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Zend\Loader\AutoloaderFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Zend\Mvc\Service\ServiceManagerConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Zend\ServiceManager\ServiceManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | error_reporting(E_ALL | E_STRICT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | chdir(__DIR__); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Test bootstrap, for setting up autoloading | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 16 |  | View Code Duplication | class Bootstrap | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected static $serviceManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public static function init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $zf2ModulePaths = array(dirname(dirname(__DIR__))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         if (($path = static::findParentPath('vendor'))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             $zf2ModulePaths[] = $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         if (($path = static::findParentPath('module')) !== $zf2ModulePaths[0]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             $zf2ModulePaths[] = $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         static::initAutoloader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         // use ModuleManager to load this module and it's dependencies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $config = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             'module_listener_options' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 'module_paths' => $zf2ModulePaths, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'modules' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 'Application' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $serviceManager = new ServiceManager(new ServiceManagerConfig()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $serviceManager->setService('ApplicationConfig', $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $serviceManager->get('ModuleManager')->loadModules(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         static::$serviceManager = $serviceManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public static function chroot() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $rootPath = dirname(static::findParentPath('module')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         chdir($rootPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public static function getServiceManager() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return static::$serviceManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     protected static function initAutoloader() | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $vendorPath = static::findParentPath('vendor'); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         if (file_exists($vendorPath.'/autoload.php')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             include $vendorPath.'/autoload.php'; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         if (! class_exists('Zend\Loader\AutoloaderFactory')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             throw new RuntimeException( | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                 'Unable to load ZF2. Run `php composer.phar install`' | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         AutoloaderFactory::factory(array( | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             'Zend\Loader\StandardAutoloader' => array( | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |                 'autoregister_zf' => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                 'namespaces' => array( | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                     __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__, | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                 ), | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             ), | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     protected static function findParentPath($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $dir = __DIR__; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $previousDir = '.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         while (!is_dir($dir . '/' . $path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $dir = dirname($dir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             if ($previousDir === $dir) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $previousDir = $dir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return $dir . '/' . $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | Bootstrap::init(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  | Bootstrap::chroot(); | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.