minphp / bridge
连接 minPHP 0.x 到 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.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-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-09-17 16:42:26 UTC
README
此库允许您无缝利用最新的命名空间 minPHP 库,同时保持与 minPHP 0.x 全局类的向后兼容性。
为什么使用这个库
此库旨在用于使用 minPHP 0.x 构建的项目,这些项目想要利用其他命名空间 minPHP 库。
安装
通过 composer 安装
composer require minphp/bridge
用法
在初始化某些库之前,桥接器需要一些信息。这通过填充并传递一个实现了 Minphp\Container\ContainerInterface
的容器来处理。
以下 minphp 0.x 中的配置文件在 minphp 1.0 中已被删除,这是需要填充容器的理由
- core.php
- database.php
- session.php
minPHP 使用 Minphp\Container\Container
,这符合此要求。以下元素必须设置
minphp.cache
包含dir
字符串dir_permission
整数(八进制)extension
字符串enabled
布尔值
minphp.config
包含dir
字符串
minphp.constants
包含APPDIR
字符串CACHEDIR
字符串COMPONENTDIR
字符串CONFIGDIR
字符串CONTROLLERDIR
字符串DS
字符串HELPERDIR
字符串HTACCESS
布尔值LANGDIR
字符串LIBDIR
字符串MINPHP_VERSION
字符串MODELDIR
字符串PLUGINDIR
字符串ROOTWEBDIR
字符串VENDORDIR
字符串VIEWDIR
字符串WEBDIR
字符串
minphp.language
包含default
字符串 'en_us'dir
字符串pass_through
布尔值
minphp.mvc
包含以下键default_controller
字符串default_structure
字符串default_view
字符串error_view
字符串view_extension
字符串cli_render_views
布尔值404_forwarding
* 布尔值*
minphp.session
包含以下键(所有都是可选的)db
包含tbl
字符串 会话数据库表tbl_id
字符串 ID 数据库字段tbl_exp
字符串 过期数据库字段tbl_val
字符串 值数据库字段ttl
整数 会话的生存时间,以秒为单位,相对于当前服务器时间(应设置为与其他 TTL 值相同的值,例如,'max(ttl, cookie_ttl)' 以正确同步客户端和服务器会话过期时间)
ttl
整数 保持会话活跃的秒数。cookie_ttl
整数 保持长期存储 cookie 活跃的秒数。session_name
字符串 会话名称。session_httponly
布尔值 启用仅 HTTP 会话 cookie。
cache
Minphp\Cache\Cacheview
View 作为工厂(每次创建新实例)loader
Loaderpdo
PDO
创建和使用容器
首先创建一个新的配置文件,命名为 services.php
,该文件将用于定义我们的服务提供者。
每个服务都定义为完全限定的类名。它可以是你想要的任何内容,只要它可以被正确地自动加载。
/config/services.php
<?php return [ 'App\\ServiceProviders\\MinphpBridge' ];
接下来,创建与我们在 services.php
中添加的匹配的服务提供者。
/app/ServiceProviders/MinphpBridge.php
注意:您可以通过在 composer.json 文件的 "autoload" 部分中定义命名空间来在此目录中自动加载类
"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 ]; }); } }
更新 init.php
更新 /lib/init.php
以使其看起来像以下内容
<?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;
删除未使用文件
有了这个桥梁,您现在可以删除项目中不再需要的 minPHP 0.x 文件。
删除以下目录和文件
- 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 (除非你在
MinphpBridge
服务提供者中使用了它) - config/session.php
- lib/ - 除了修改后的
init.php
文件
问:为什么我们保留
init.php
文件?
答:因为
index.php
在 minPHP 0.x 中加载它,我们正在保持向后兼容性。如果init.php
在其他任何地方都没有被加载,那么你可以将其内容放入另一个文件中,并更新你的index.php
文件以加载该文件。这取决于你。