public
Last active

Bootstrapping a multi-environment ZF 1.x application (please use Composer and Composer's autoloader instead)

  • Download Gist
application.httpd
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
<VirtualHost *:80>
ServerName ##APP_DOMAIN##
ServerAlias www.##APP_DOMAIN##
ServerAlias ##APPLICATION_ENV##.www.##APP_DOMAIN##
ServerAlias ##APPLICATION_ENV##.##APP_DOMAIN##
ServerAdmin admin@##APP_DOMAIN##
 
SetEnv APPLICATION_ENV ##APPLICATION_ENV##
SetEnv SERVER_HOSTNAME ##SERVER_HOSTNAME##
Header append X-Host-List "%{SERVER_HOSTNAME}e"
 
RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}$1 [L,R]
</VirtualHost>
 
<VirtualHost *:443>
ServerName ##APP_DOMAIN##
ServerAlias www.##APP_DOMAIN##
ServerAlias ##APPLICATION_ENV##.www.##APP_DOMAIN##
ServerAlias ##APPLICATION_ENV##.##APP_DOMAIN##
ServerAdmin admin@##APP_DOMAIN##
 
SetEnv APPLICATION_ENV ##APPLICATION_ENV##
SetEnv SERVER_HOSTNAME ##SERVER_HOSTNAME##
Header append X-Host-List "%{SERVER_HOSTNAME}e"
 
SSLEngine On
SSLOptions +StrictRequire
SSLCertificateFile /etc/ssl/certs/##APP_DOMAIN##.crt
SSLCertificateKeyFile /etc/ssl/private/##APP_DOMAIN##.key
SSLCertificateChainFile /etc/ssl/certs/intermediate.crt
 
DocumentRoot ##BASE_PATH##/src/public
<Directory ##BASE_PATH##/src/public/>
Options FollowSymLinks
AllowOverride None
Order Allow,Deny
Allow from All
</Directory>
 
<Location />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</Location>
 
<FilesMatch "\.(htm|html|css|js|php)$">
AddDefaultCharset UTF-8
DefaultLanguage en-US
</FilesMatch>
 
ErrorLog ##BASE_PATH##/tmp/shared/log/errors.log
CustomLog ##BASE_PATH##/tmp/shared/log/access.log common
</VirtualHost>
autoload.php.dist
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
<?php
/**
* Autoload Everything (PHP 5.3)
*
* @package ...
* @copyright Copyright (c) ####-#### Something, Inc. - All rights reserved.
* @author Wil Moore III <wil.moore@wilmoore.com>
*/
require_once __DIR__.'/src/vendor/Symfony/Component/ClassLoader/UniversalClassLoader.php';
 
use Symfony\Component\ClassLoader\UniversalClassLoader;
 
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
'm3' => __DIR__.'/src/library',
'Symfony' => __DIR__.'/src/vendor',
'DoctrineExtensions' => __DIR__.'/src/vendor',
'Doctrine' => __DIR__.'/src/vendor',
'Reactq' => __DIR__.'/src/vendor/reactq/src/lib',
'Zend' => __DIR__.'/src/vendor/zend/v2.0.0dev2/library',
));
 
$loader->registerPrefixes(array(
'm3_common_view_helper_' => __DIR__.'/src/library/m3/common/view',
'Zend_' => __DIR__.'/src/vendor/zend/v1.11.4/library',
'PHPExcel' => __DIR__.'/src/vendor/php_excel',
'Twig_' => __DIR__.'/src/vendor/twig/v1.0.0/lib',
'ZFDebug_' => __DIR__.'/src/vendor',
));
 
$loader->register();
 
// unfortunately, we need to add (repeat) the zend library directory to the include_path in order for ZF1 to function correctly (plugin-loading, etc)
$realPaths[] = __DIR__.'/src/vendor/zend/v1.11.4/library';
$realPaths[] = __DIR__.'/src/vendor';
$realPaths[] = get_include_path();
 
$includePath = join(PATH_SEPARATOR, $realPaths);
set_include_path($includePath);
 
// loading swift mailer's dependency injection mapping
require __DIR__ . '/src/vendor/swiftmailer/v4.1.0/lib/swift_required.php';
bootstrap.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
<?php
/**
* General environment setup for all contexts
* Defines paths, auto-loading, and bootstrapped resources/dependencies
*
* @package ...
* @copyright ...
* @author ...
*/
/*******************************************************************************
initial error reporting (application should override this based on environment)
*******************************************************************************/
 
// always set to (-1) to report all errors
error_reporting(-1);
 
// set to 'syslog' so all error logs go to /var/log/messages by default
ini_set('error_log', 'syslog');
ini_set('html_errors', false);
ini_set('display_errors', false);
 
// set timezone to 'Etc/UTC'
ini_set('date.timezone', 'Etc/UTC');
date_default_timezone_set('Etc/UTC');
 
/*******************************************************************************
command-line option parsing
*******************************************************************************/
 
//$argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
if (! empty($_SERVER['argv'])) {
$argv=& $_SERVER['argv'];
 
foreach ($argv as $argKey => $arg) {
$paramKey = $argKey + 1;
 
if ($arg == '--environment' && isset($argv[$paramKey])) {
putenv(sprintf('APPLICATION_ENV=%s', $argv[$paramKey]));
unset($argv[$argKey], $argv[$paramKey]);
}
}
 
unset($argKey, $paramKey, $arg, $env);
}
 
/*******************************************************************************
application paths
*******************************************************************************/
 
// application name
defined('APPLICATION_NAME')
|| define('APPLICATION_NAME', 'your-application-name');
 
// root project path
defined('BASE_PATH')
|| define('BASE_PATH', __DIR__);
 
// source directory path
defined('SOURCE_PATH')
|| define('SOURCE_PATH', realpath(BASE_PATH . DIRECTORY_SEPARATOR . 'src'));
 
// configs directory path
defined('CONFIGS_PATH')
|| define('CONFIGS_PATH', realpath(SOURCE_PATH . DIRECTORY_SEPARATOR . 'configs'));
 
// web application path
defined('WEBAPP_PATH')
|| define('WEBAPP_PATH', realpath(SOURCE_PATH . DIRECTORY_SEPARATOR . 'webapp'));
 
/**
* application environment
*
* Check for environment override, otherwise, get the configuration parameter
* value. Failing that, default to common. This can be set via:
* (1) Virtual Host Configuration (SetEnv APPLICATION_ENV ##APPLICATION_ENV##)
* (2) Command-Line Scripts (APPLICATION_ENV=local mqueue.php -d)
* (3) Command-Line Script Args (mqueue.php --environment local)
*/
if (! defined('APPLICATION_ENV')) {
if (false !== getenv('APPLICATION_ENV_OVERRIDE')) {
define('APPLICATION_ENV', getenv('APPLICATION_ENV_OVERRIDE'));
} else {
define('APPLICATION_ENV', getenv('APPLICATION_ENV') ?: 'common');
}
}
 
/*******************************************************************************
define constant 'APPLICATION_CONFIG' (application configuration file)
*******************************************************************************/
 
$files[] = realpath(CONFIGS_PATH.'/application.ini');
$files[] = realpath(CONFIGS_PATH.'/application.ini.dist');
 
foreach ($files as $file) {
is_readable($file) && !defined('APPLICATION_CONFIG') && define('APPLICATION_CONFIG', $file);
}
unset($files, $file);
 
if (!defined('APPLICATION_CONFIG')) {
throw new RuntimeException('Unable to locate an application configuration file.');
}
 
/*******************************************************************************
setup autoloader
*******************************************************************************/
 
$files[] = realpath(__DIR__.'/autoload.php');
$files[] = realpath(__DIR__.'/autoload.php.dist');
 
foreach ($files as $file) {
is_readable($file) && !defined('AUTOLOADER_PATH') && define('AUTOLOADER_PATH', $file);
}
unset($files, $file);
 
if (!defined('AUTOLOADER_PATH')) {
throw new RuntimeException('Unable to locate an autoloader file.');
}
 
require_once AUTOLOADER_PATH;
 
/*******************************************************************************
setup root error/exception handler
*******************************************************************************/
 
m3\common\error\Exception::handle();
index.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<?php
/**
* Public entry-point for the web application
*
* @package ...
* @copyright ...
* @author ...
*/
// setup base environment
require_once realpath(dirname(__DIR__) . '/../bootstrap.php');
 
/*******************************************************************************
initial error reporting
*******************************************************************************/
 
// display errors
ini_set('display_errors', false);
 
// html errors
ini_set('html_errors', true);
 
/******************************************************************************
Configure Zend_Application, bootstrap, and run
******************************************************************************/
 
// setup Zend_Application with application environment and configuration file paths
$application = new Zend_Application(APPLICATION_ENV, APPLICATION_CONFIG);
 
// bootstrap then start the MVC handler
$application->bootstrap()->run();
readme.rst
reStructuredText

Strip require_once calls with find and sed (Zend Framework) -- assumes the use of the "Symfony Universal Autoloader"

REF: http://framework.zend.com/manual/en/performance.classloading.html#performance.classloading.striprequires.sed

% cd path/to/ZendFramework/library

-- MAC -- % find . -name '*.php' -print0 | xargs -0 sed -E -i '' 's/(require_once)/// 1/g'

-- LINUX -- % find . -name '*.php' -print0 | xargs -0 sed --regexp-extended --in-place 's/(require_once)/// 1/g'

-- COMMENT out the following block -- % vim +319 Zend/Application.php

if (!class_exists($class, false)) {

require_once $path; if (!class_exists($class, false)) {

throw new Zend_Application_Exception('Bootstrap class not found');

}

}

-- NOTES -- Doing the above and using the Symfony autoloader not only increases performance; it also allows one to run ZF1 with only the ZF library in the include_path. Everything else can be autoloaded. This also mitigates the need to add Autoloadernamespaces[] to the application configuration file.

NOTE: you can also create other entry-points (like index.php) for things such as console (CLI) scripts. Any entry-point script just needs to require the "bootstrap.php" and make the appropriate changes to "display_errors", etc.

This is should be considered deprecated in favor of Composer and its autoloader.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.