talmira/envelophp

Parse dotenv file for native php applications

v2.0.0 2021-04-27 22:24 UTC

This package is auto-updated.

Last update: 2024-03-11 06:58:47 UTC


README

alt text

License GitHub stars GitHub forks GitHub issues

Latest Stable Version Latest Unstable Version Total Downloads

GitHub code size in bytes License

What is envelophp?

This package will help you to keep your database in one place and use them across your native php application.

envelophp use .env file to store database credentials and parse its content to be used in database connection.

Currently, envelophp supports mysql databases only.

Installation

Simple installation using Composer:

composer require talmira/envelophp

Setup

  • Create .env file:

Navigate to your project root folder then create .env file using one of the following methods:

1- Use curl to create .env file

  curl -LJ -o ".env" https://gist.github.com/theahmedlatif/3c5c7fd454f48898d7660bef555aca31/raw

2- Use wget to create .env file

   wget --output-document=.env --no-check-certificate --content-disposition https://gist.github.com/theahmedlatif/3c5c7fd454f48898d7660bef555aca31/raw

YOU MUST ADD .env TO YOUR .gitignore FILE TO AVOID PUBLISHING YOUR CREDENTIALS TO THE PUBLIC

  • Modify .env file:

#Database#
HOST = "localhost"
DATABASE_DSN = "mysql"
DATABASE_NAME = "sample_db"
DATABASE_USERNAME = "sample_username"
DATABASE_PASSWORD = "sample_password"

Test

envelophp provides its own unit test cases:

  • Fetch information from .env:

this test case validate .env file location is the pointed location by Envelope Class and ability to read variables.

php vendor/talmira/envelophp/test/FetchDotEnv.php
  • Test database connection:

this test case test database connection using .env variables.

php vendor/talmira/envelophp/test/MysqlDBTest.php

Usage

1- Create Database

  • So if you don't have a created Database you can use this secondary feature of envelophp, createDatabase() uses database_name mentioned in .env file to create Mysql Database.
<?php
require_once dirname(__FILE__).'/vendor/autoload.php';
use Envelope\Database\MysqlDatabase;

$database = new MysqlDatabase();
$database->createDatabase();

2- Create Connection

  • Simply require autoload.php in your file where you want to connect to your database.
  • Then use the MysqlDatabase namespace
  • Now you are ready to create a new instance of MysqlDatabase.
  • to make a database connection call getConnection().
<?php
require_once dirname(__FILE__).'/vendor/autoload.php';
use Envelope\Database\MysqlDatabase;

$database = new MysqlDatabase();
$database->getConnection();

3- Close database connection by calling closeConnection() method:

$database->closeConnection();

Conclusion

using envelophp in your native php project is very simple and will apply DRY concept.