Skip to content

Programmatically configure your php.ini!

License

Notifications You must be signed in to change notification settings

Piagrammist/easy-php-ini

Repository files navigation

Easy php.ini

A quick way to prepare your php.ini! ;-)

License CI status Min PHP version

Table of Contents

Usage

  • Download the template script:

    curl -o setup-ini.php https://raw.githubusercontent.com/Piagrammist/easy-php-ini/main/scripts/setup-ini.php
  • Execute the script using the target php binary:

    C:\php\php.exe setup-ini.php

Tip

For Windows 8.1 and below, you can manually download curl.exe.

Config

Calling the setup() method will read, process and write the ini. process() can be used instead if you do not wish to output to a file.

By default nothing happens!

<?php

use EasyIni\Processor;

$ini = new Processor;
$ini->setup();

/*
 * The needed paths will be automatically detected,
 *   but if you "really" needed to specify custom input/output php.ini paths,
 *   you can do so:
 */
$ini->setup(
    '/home/rz/input.ini',
    '/home/rz/output.ini',
);

$output = $ini->process('/home/rz/input.ini');

Environment

This is only useful for Windows users. If no php.ini already exists, php.ini-{development,production} will be used as the template depending on the env value.

Default is set to development.

<?php

$ini->development()
    ->production(); // overrides the previous

// switches to `production` mode
$ini->development(false);

Extensions

Extension handling is only supported on Windows!

<?php

use EasyIni\Ini\EntryState;
use EasyIni\Options\ExtensionOptions;

$extension = new ExtensionOptions;
$extension->setExtensions([
    'curl',
    'ftp',
    'mysqli',
    'zip',
]);

/*
 * Comment/disables the extensions.
 *
 * Note that this overrides the previous.
 *   You'd need to make a separate processor-
 *   if you need to add and remove extensions at the same time!
 */
$extension->setExtensions([
    'curl',
    'ftp',
    'mysqli',
    'zip',
], EntryState::COMMENT);

// A custom `ext dir` could be set, but is not necessary!
$extension->setExtensionDir('C:\my\custom\extension\path');

$ini->setExtension($extension);

Tip

If any extension provided, the extension_dir entry will be automatically uncommented.

Error Handling

<?php

use EasyIni\Options\ErrorHandlingOptions;

$errorHandling = new ErrorHandlingOptions;
$errorHandling
    ->setHtmlErrors()
    ->setDisplayErrors()
    ->setDisplayStartupErrors()
    ->setLogErrors(false);

$ini->setErrorHandling($errorHandling);

Resource Limits

<?php

use EasyIni\Ini\EntryState;
use EasyIni\Options\ResourceLimitOptions;

$limits = new ResourceLimitOptions;
// comments out the entry to use the default
$limits->setMaxExecutionTime(state: EntryState::COMMENT);
$limits
    ->setMaxInputTime(30)
    ->setMaxInputVars(100)
    ->setMemoryLimit('256M');

$ini->setResourceLimits($limits);

Disable Functions and Classes

Internal php functions/classes can be disabled by providing a DisableOptions:

<?php

use EasyIni\Options\DisableOptions;

$disable = new DisableOptions;
$disable->setFunctions(['exec', 'shell_exec']);
// WARN: `a` is not a class and will be ignored. (strict mode)
$disable->setClasses(['ZipArchive', 'a']);

// The strict behavior can be disabled using:
$disable->setStrict(false);

$ini->setDisable($disable);

Phar

<?php

use EasyIni\Options\PharOptions;

$phar = (new PharOptions)
    ->setReadonly(false)
    ->setRequireHash(true);
// has strict mode for paths.
$phar->setCacheList(['/var/www/html/vendor.phar']);

// The strict behavior can be disabled using:
$phar->setStrict(false);

$ini->setPhar($phar);

Just In Time Compilation

JIT compilation can be enabled and configured using the setJit() method, which accepts either a boolean or JitOptions object:

<?php

use EasyIni\Ini\EntryState;
use EasyIni\Options\JitOptions;

$jit = new JitOptions;

$jit->setEnabled(false, EntryState::COMMENT);
$jit->setEnabledCli();

$jit->setFlags('tracing'); // default
$jit->setFlags(1254); // same as 'tracing'

$jit->setBufferSize('64M'); // default
$jit->setBufferSize(67_108_864); // same as '64M'

$ini->setJit($jit);

// --------
// Or the quick way:
$ini->setJit();
$ini->setJit(false);

Logging

The logger level could be changed anywhere in the program using:

<?php

EasyIni\Logger::setLevel(Monolog\Level::Debug);

TODO

  • Add tests.
  • Add CI.
  • Add exception handling.
  • Add Linux support.
  • Add Logging.
  • Expand project into files and release PHAR.
  • Add dependabot for dependency update checks.
  • Automate PHAR release using CD.
  • Add error_reporting entry to ErrorHandlingProcessor.

About

Programmatically configure your php.ini!

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages