Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tomaskraus/php-includer
A minimalistic PHP library for include/require
https://github.com/tomaskraus/php-includer
Last synced: about 1 month ago
JSON representation
A minimalistic PHP library for include/require
- Host: GitHub
- URL: https://github.com/tomaskraus/php-includer
- Owner: tomaskraus
- License: bsd-3-clause
- Created: 2016-08-30T17:00:53.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-08-18T14:33:09.000Z (over 7 years ago)
- Last Synced: 2024-04-18T03:18:04.703Z (9 months ago)
- Language: PHP
- Size: 15.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# php-includer
***NOTE:*** This project is ***Obsolete***. Use the [path-utils](https://github.com/tomaskraus/path-utils) library - it does one thing and does it good.
A minimalistic PHP library/framework for include/require
## features
* application root path
* smart path join
* "bootstrap" file auto include## Installation
Via composer:
```
composer require tomaskraus/php-includer
```
, or add this snippet to `composer.json`
```json
"require": {
"tomaskraus/php-includer": "^0.4"
},
```## php-includer usage options
1. **Use as a library**: Create a new `PI` (php includer) instance with your application root path as a parameter.
2. **Use as a framework**: Include `piLoader.php` in your requested page.
It guesses your application root path and creates a new PI instance (a `$pi` variable) with that path guessed.
It also automatically includes an user-defined "bootstrap" file (`pi.bootstrap.php`), an ideal place for your specific initialization piece of code.### application root path guess logic
1. `piLoader.php` assumes that it is in a `vendor/tomaskraus/php-include` directory.
2. `piLoader.php` assumes that a `vendor` directory is in the application root directory.## php-includer framework examples
Assume we have our php application in `/var/www/myApp`. A `/var/www/myApp` is our application root path (`./`).
example 1: php-includer in the `./index.php` file
```php
path(); //echoes "/var/www/myApp/"//we can create new, non-existing path strings, based on a web application root
$pi->path("conf/file-to-be-created.php"); //returns "/var/www/myapp/conf/file-to-be-created.php".//path-safe include, wherever you are
include $pi->path("myLib/utils.php"); //includes /var/www/myapp/myLib/utils.php//smart path join, fixes missing or too many separators
PI::joinPath("myapp/", "/dist/app.zip"); //returns "myapp/dist/app.zip"
PI::joinPath("/var/www", "dist/app.zip"); //returns "/var/www/dist/app.zip", preserves a root slash
//join Windows path
PI::joinPath("C:\\www\\", "/dist/app.zip"); //returns "C:\\www/dist/app.zip", mixed result for Windows path (still works in PHP)
PI::joinPath("C:\\www", "dist/app.zip"); //returns the same...
```
**example 2**: php-includer in the `./login/index.php` file
```php
path(); //echoes "/var/www/myapp/"//path-safe include, wherever you are (now we are in the "login" subdirectory)
include $pi->path("myLib/utils.php"); //includes /var/www/myapp/myLib/utils.php```
### bootstrap file
You can create a `pi.bootstrap.php` file in the application root directory and put whatever you want in it.
That bootstrap file will be automatically included, wherever you include/require `piLoader`, regardless of (sub)directory.
A bootstrap file is an ideal place for your specific initialization piece of code.**note:** this bootstrap file has nothing to do with the [Bootstrap CSS framework](http://getbootstrap.com/)
`pi.bootstrap.php` file example: assume we have a `pi.bootstrap.php` file in `/var/www/myapp` directory:
```php
path("conf/config.php"); //includes /var/www/myapp/conf/config.php//This bootstrap file is a right place to write the class auto loader here
$autoLoaderFile = __DIR__ . "/vendor/autoload.php";
if (file_exists($autoLoaderFile)) {
require_once $autoLoaderFile;
}//other, globally needed stuff...
```
This bootstrap file will be automatically included in both `./index.php` and `./login/index.php` files from previous examples.