Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Created April 22, 2011 19:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wilmoore/937433 to your computer and use it in GitHub Desktop.
Save wilmoore/937433 to your computer and use it in GitHub Desktop.
Bootstrapping a multi-environment ZF 1.x application (please use Composer and Composer's autoloader instead)

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.

<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>
<?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';
<?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();
<?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();
@wilmoore
Copy link
Author

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.

@wilmoore
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment