minphp / bridge
Bridges minPHP 0.x to minPHP 1.x
Requires
- php: >=5.4.0
- minphp/acl: ^2.0
- minphp/cache: ^1.0
- minphp/configure: ^2.0
- minphp/container: ^2.1
- minphp/date: ^1.0
- minphp/form: ^1.0
- minphp/html: ^1.0
- minphp/input: ^1.0
- minphp/javascript: ^1.0
- minphp/language: ^1.1
- minphp/pagination: ^1.0
- minphp/record: ^3.0
- minphp/session: ^1.2.1
- minphp/xml: ^1.0
Requires (Dev)
- php-coveralls/php-coveralls: ~1.0
- phpunit/phpunit: ~4.0
- squizlabs/php_codesniffer: ~2.2
- dev-master
- 3.2.x-dev
- 3.2.0
- 3.1.x-dev
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.0.x-dev
- 3.0.2
- 3.0.1
- 3.0.0
- 2.2.x-dev
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.x-dev
- 2.1.0
- 2.0.x-dev
- 2.0.0
- 1.0.x-dev
- 1.0.1
- 1.0.0
- 1.0.0-rc14
- 1.0.0-rc13
- 1.0.0-rc12
- 1.0.0-rc11
- 1.0.0-rc10
- 1.0.0-rc9
- 1.0.0-rc8
- 1.0.0-rc7
- 1.0.0-rc6
- 1.0.0-rc5
- 1.0.0-rc4
- 1.0.0-rc3
- 1.0.0-rc2
- 1.0.0-rc1
- dev-CORE-5229
- dev-CORE-4853
- dev-update-match-method
- dev-set-collation-based-on-config
- dev-session-setoptions-error
- dev-dispatcher-allow-controller-magic-methods
- dev-set-exception-on-error
- dev-controller-preaction-dispatcher
- dev-php7-errors
This package is auto-updated.
Last update: 2024-11-12 20:17:07 UTC
README
This library allows you to seamlessly take advantage of newer namespaced minPHP libraries while at the same time maintaining backwards compatibility with minPHP 0.x global classes.
Why use this library
This library is intended for projects that are built using minPHP 0.x that want to take advantage of other namespaced minPHP libraries.
Installation
Install via composer:
composer require minphp/bridge
Usage
The bridge requires some information before it's able to initialize some
libraries. This is handled by populating and passing in a container that
implements Minphp\Container\ContainerInterface
.
The following config files in minphp 0.x were removed in minphp 1.0, which is what necessitates populating the container:
- core.php
- database.php
- session.php
minPHP uses the Minphp\Container\Container
, which meets this requirement. The
following elements are required to be set:
minphp.cache
array containing:dir
stringdir_permission
int (octal)extension
stringenabled
bool
minphp.config
array containing:dir
string
minphp.constants
array containing:APPDIR
stringCACHEDIR
stringCOMPONENTDIR
stringCONFIGDIR
stringCONTROLLERDIR
stringDS
stringHELPERDIR
stringHTACCESS
boolLANGDIR
stringLIBDIR
stringMINPHP_VERSION
stringMODELDIR
stringPLUGINDIR
stringROOTWEBDIR
stringVENDORDIR
stringVIEWDIR
stringWEBDIR
string
minphp.language
array containing:default
string 'en_us'dir
stringpass_through
bool
minphp.mvc
array containing the following keys:default_controller
stringdefault_structure
stringdefault_view
stringerror_view
stringview_extension
stringcli_render_views
bool404_forwarding
* bool*
minphp.session
array containing the following keys (all optional):db
array containing:tbl
string The session database tabletbl_id
string The ID database fieldtbl_exp
string The expiration database fieldtbl_val
string The value database fieldttl
int The session time-to-live, in seconds, relative to current server time (should be set to the same value as the other TTLs, e.g., 'max(ttl, cookie_ttl)' to correctly sync client and server session expirations)
ttl
int Number of seconds to keep a session alive.cookie_ttl
int Number of seconds to keep long storage cookie alive.session_name
string Name of the session.session_httponly
bool True to enable HTTP only session cookies.
cache
Minphp\Cache\Cacheview
View As a factory (new instance each time)loader
Loaderpdo
PDO
Creating and Using the Container
First create a new config file called services.php
that will be used to
define our service providers.
Each service is defined as the fully qualified class name. It can be whatever you want as long as it can be properly autoloaded.
/config/services.php
<?php return [ 'App\\ServiceProviders\\MinphpBridge' ];
Next, create the service provider that matches the one we added to services.php
.
/app/ServiceProviders/MinphpBridge.php
Note: You can auotload classes in this directory by defining the namespace in your composer.json file under the "autoload" section like so:
"autoload": {
"psr-4": {
"App\\ServiceProviders\\": "app/ServiceProviders/"
}
}
<?php namespace App\ServiceProviders; use Pimple\ServiceProviderInterface; use Pimple\Container; use Cache; use View; use Loader; use PDO; use Configure; class MinphpBridge implements ServiceProviderInterface { private $container; /** * {@inheritdoc} */ public function register(Container $container) { $this->container = $container; $this->registerCache(); $this->registerConfig(); $this->registerConstants(); $this->registerLanguage(); $this->registerMvc(); $this->registerSession(); $container->set('cache', function ($c) { return Cache::get(); }); $container->set('view', $container->factory(function ($c) { return new View(); })); $container->set('loader', function ($c) { $constants = $c->get('minphp.constants'); $loader = Loader::get(); $loader->setDirectories([ $constants['ROOTWEBDIR'] . $constants['APPDIR'], 'models' => $constants['MODELDIR'], 'controllers' => $constants['CONTROLLERDIR'], 'components' => $constants['COMPONENTDIR'], 'helpers' => $constants['HELPERDIR'], 'plugins' => $constants['PLUGINDIR'] ]); return $loader; }); $container->set('pdo', function ($c) { Configure::load('database'); $dbInfo = Configure::get('Database.profile'); return new PDO( $dbInfo['driver'] . ':dbname=' . $dbInfo['database'] . ';host=' . $dbInfo['host'] . ( isset($dbInfo['port']) ? ':' . $dbInfo['port'] : '' ), $dbInfo['user'], $dbInfo['pass'] ); }); } private function registerCache() { $this->container->set('minphp.cache', function ($c) { return [ 'dir' => $c->get('minphp.constants')['CACHEDIR'], 'dir_permissions' => 0755, 'extension' => '.html', 'enabled' => true ]; }); } private function registerConfig() { $this->container->set('minphp.config', function ($c) { return [ 'dir' => $c->get('minphp.constants')['CONFIGDIR'] ]; }); } private function registerConstants() { $this->container->set('minphp.constants', function ($c) { $rootWebDir = realpath(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR; $appDir = 'app' . DIRECTORY_SEPARATOR; $htaccess = file_exists($rootWebDir . '.htaccess'); $script = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : ( isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : null ); $webDir = ( !$htaccess ? $script : ( ($path = dirname($script)) === '/' || $path == DIRECTORY_SEPARATOR ? '' : $path ) ) . '/'; if ($webDir === $rootWebDir) { $webDir = '/'; } return [ 'APPDIR' => $appDir, 'CACHEDIR' => $rootWebDir . 'cache' . DIRECTORY_SEPARATOR, 'COMPONENTDIR' => $rootWebDir . 'components' . DIRECTORY_SEPARATOR, 'CONFIGDIR' => $rootWebDir . 'config' . DIRECTORY_SEPARATOR, 'CONTROLLERDIR' => $rootWebDir . $appDir . 'controllers' . DIRECTORY_SEPARATOR, 'DS' => DIRECTORY_SEPARATOR, 'HELPERDIR' => $rootWebDir . 'helpers' . DIRECTORY_SEPARATOR, 'HTACCESS' => $htaccess, 'LANGDIR' => $rootWebDir . 'language' . DIRECTORY_SEPARATOR, 'LIBDIR' => $rootWebDir . 'lib' . DIRECTORY_SEPARATOR, 'MINPHP_VERSION' => '1.0.0', 'MODELDIR' => $rootWebDir . $appDir . 'models' . DIRECTORY_SEPARATOR, 'PLUGINDIR' => $rootWebDir . 'plugins' . DIRECTORY_SEPARATOR, 'ROOTWEBDIR' => $rootWebDir, 'VEDNORDIR' => $rootWebDir . 'vendors' . DIRECTORY_SEPARATOR, 'VIEWDIR' => $rootWebDir . $appDir . 'views' . DIRECTORY_SEPARATOR, 'WEBDIR' => $webDir ]; }); } private function registerLanguage() { $this->container->set('minphp.language', function ($c) { return [ 'default' => 'en_us', 'dir' => $c->get('minphp.constants')['LANGDIR'], 'pass_through' => false ]; }); } private function registerMvc() { $this->container->set('minphp.mvc', function ($c) { return [ 'default_controller' => 'main', 'default_structure' => 'structure', 'default_view' => 'default', 'error_view' => 'errors', 'view_extension' => '.pdt', 'cli_render_views' => false, '404_forwarding' => false ]; }); } private function registerSession() { $this->container->set('minphp.session', function ($c) { return [ 'db' => [ 'tbl' => 'sessions', 'tbl_id' => 'id', 'tbl_exp' => 'expire', 'tbl_val' => 'value' ], 'ttl' => 1800, // 30 mins 'cookie_ttl' => 604800, // 7 days 'session_name' => 'sid', 'session_httponly' => true ]; }); } }
Updating init.php
Update /lib/init.php
so it looks like the following:
<?php error_reporting(-1); // include autoloader require_once dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; // Fetch available services $services = require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'services.php'; // Initialize $container = new Minphp\Container\Container(); // Set services foreach ($services as $service) { $container->register(new $service()); } // Run bridge $bridge = Minphp\Bridge\Initializer::get(); $bridge->setContainer($container); $bridge->run(); // Set the container Configure::set('container', $container); return $container;
Removing Unused Files
With this bridge in place, you can now remove minPHP 0.x files that are no longer required in your project.
Remove the following directories and files:
- components/acl/
- components/input/
- components/record/
- components/session/
- helpers/date/
- helpers/form/
- helpers/html/
- helpers/javascript/
- helpers/pagination/
- helpers/xml/
- config/core.php
- config/database.php (unless you used it in your
MinphpBridge
service provider) - config/session.php
- lib/ - except the modified
init.php
file
Q: Why do we keep the
init.php
file?
A: Because
index.php
loads it in minPHP 0.x, and we're maintaining backwards compatibility. Ifinit.php
isn't loaded anywhere else, then you could put its contents in another file and update yourindex.php
file to load that file instead. It's up to you.