# YiiRequirementChecker

> YiiRequirementChecker allows checking, if current system meets the requirements for running the Yii application.

[API reference](/docs/api/index.md) › [Global namespace](/docs/api/global/index.md) › YiiRequirementChecker

| class | `YiiRequirementChecker` |
| --- | --- |
| Since | 2.0 |
| Source | [`framework/requirements/YiiRequirementChecker.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/requirements/YiiRequirementChecker.php#L56) |

YiiRequirementChecker allows checking, if current system meets the requirements for running the Yii application.

This class allows rendering of the check report for the web and console application interface.

Example:

```
require_once 'path/to/YiiRequirementChecker.php';
$requirementsChecker = new YiiRequirementChecker();
$requirements = array(
    array(
        'name' => 'PHP Some Extension',
        'mandatory' => true,
        'condition' => extension_loaded('some_extension'),
        'by' => 'Some application feature',
        'memo' => 'PHP extension "some_extension" required',
    ),
);
$requirementsChecker->checkYii()->check($requirements)->render();
```

If you wish to render the report with your own representation, use [`getResult()`](/docs/api/global/yiirequirementchecker/index.md#method-getResult) instead of [`render()`](/docs/api/global/yiirequirementchecker/index.md#method-render)

Requirement condition could be in format "eval:PHP expression".
In this case specified PHP expression will be evaluated in the context of this class instance.
For example:

```
$requirements = array(
    array(
        'name' => 'Upload max file size',
        'condition' => 'eval:$this->checkUploadMaxFileSize("5M")',
    ),
);
```

Note: this class intentionally keeps a conservative standalone style
for compatibility with requirement-check bootstrap environments.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-result"></span>`$result` | `array\|null` | The check results, this property is for internal usage only. Default: `null`. | `YiiRequirementChecker` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`check()`](#method-check) | Check the given requirements, collecting results into internal field. | `YiiRequirementChecker` |
| [`checkPhpExtensionVersion()`](#method-checkPhpExtensionVersion) | Checks if the given PHP extension is available and its version matches the given one. | `YiiRequirementChecker` |
| [`checkPhpIniOff()`](#method-checkPhpIniOff) | Checks if PHP configuration option (from php.ini) is off. | `YiiRequirementChecker` |
| [`checkPhpIniOn()`](#method-checkPhpIniOn) | Checks if PHP configuration option (from php.ini) is on. | `YiiRequirementChecker` |
| [`checkUploadMaxFileSize()`](#method-checkUploadMaxFileSize) | Checks if upload max file size matches the given range. | `YiiRequirementChecker` |
| [`checkYii()`](#method-checkYii) | Performs the check for the Yii core requirements. | `YiiRequirementChecker` |
| [`compareByteSize()`](#method-compareByteSize) | Compare byte sizes of values given in the verbose representation, like '5M', '15K' etc. | `YiiRequirementChecker` |
| [`evaluateExpression()`](#method-evaluateExpression) | Evaluates a PHP expression under the context of this class. | `YiiRequirementChecker` |
| [`getByteSize()`](#method-getByteSize) | Gets the size in bytes from verbose size representation. | `YiiRequirementChecker` |
| [`getNowDate()`](#method-getNowDate) | Returns the now date if possible in string representation. | `YiiRequirementChecker` |
| [`getResult()`](#method-getResult) | Return the check results. | `YiiRequirementChecker` |
| [`getServerInfo()`](#method-getServerInfo) | Returns the server information. | `YiiRequirementChecker` |
| [`normalizeRequirement()`](#method-normalizeRequirement) | Normalizes requirement ensuring it has correct format. | `YiiRequirementChecker` |
| [`render()`](#method-render) | Renders the requirements check result. | `YiiRequirementChecker` |
| [`renderViewFile()`](#method-renderViewFile) | Renders a view file. | `YiiRequirementChecker` |
| [`usageError()`](#method-usageError) | Displays a usage error. | `YiiRequirementChecker` |

## Method details

### check() <span id="method-check"></span>

```php
public function check(array|string $requirements): $this
```

Check the given requirements, collecting results into internal field.

This method can be invoked several times checking different requirement sets.
Use [`getResult()`](/docs/api/global/yiirequirementchecker/index.md#method-getResult) or [`render()`](/docs/api/global/yiirequirementchecker/index.md#method-render) to get the results.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$requirements` | `array\|string` | Requirements to be checked. If an array, it is treated as the set of requirements; If a string, it is treated as the path of the file, which contains the requirements; |

**Returns**

`$this`: Self instance.

### checkPhpExtensionVersion() <span id="method-checkPhpExtensionVersion"></span>

```php
public function checkPhpExtensionVersion(string $extensionName, string $version, string $compare = '>='): bool
```

Checks if the given PHP extension is available and its version matches the given one.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$extensionName` | `string` | PHP extension name. |
| `$version` | `string` | Required PHP extension version. |
| `$compare` | `string` | Comparison operator, by default '>=' |

**Returns**

`bool`: If PHP extension version matches.

### checkPhpIniOff() <span id="method-checkPhpIniOff"></span>

```php
public function checkPhpIniOff(string $name): bool
```

Checks if PHP configuration option (from php.ini) is off.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Configuration option name. |

**Returns**

`bool`: Option is off.

### checkPhpIniOn() <span id="method-checkPhpIniOn"></span>

```php
public function checkPhpIniOn(string $name): bool
```

Checks if PHP configuration option (from php.ini) is on.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Configuration option name. |

**Returns**

`bool`: Option is on.

### checkUploadMaxFileSize() <span id="method-checkUploadMaxFileSize"></span>

```php
public function checkUploadMaxFileSize(string|null $min = \null, string|null $max = \null): bool
```

Checks if upload max file size matches the given range.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$min` | `string\|null` | Verbose file size minimum required value, pass null to skip minimum check. |
| `$max` | `string\|null` | Verbose file size maximum required value, pass null to skip maximum check. |

**Returns**

`bool`: Success.

### checkYii() <span id="method-checkYii"></span>

```php
public function checkYii(): YiiRequirementChecker
```

Performs the check for the Yii core requirements.

**Returns**

[`YiiRequirementChecker`](/docs/api/global/yiirequirementchecker/index.md): Self instance.

### compareByteSize() <span id="method-compareByteSize"></span>

```php
public function compareByteSize(string $a, string $b, string $compare = '>='): bool
```

Compare byte sizes of values given in the verbose representation,
like '5M', '15K' etc.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$a` | `string` | First value. |
| `$b` | `string` | Second value. |
| `$compare` | `string` | Comparison operator, by default '>='. |

**Returns**

`bool`: Comparison result.

### evaluateExpression() <span id="method-evaluateExpression"></span>

```php
public function evaluateExpression(string $expression): mixed
```

Evaluates a PHP expression under the context of this class.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$expression` | `string` | A PHP expression to be evaluated. |

**Returns**

`mixed`: The expression result.

### getByteSize() <span id="method-getByteSize"></span>

```php
public function getByteSize(string $verboseSize): int
```

Gets the size in bytes from verbose size representation.

For example: '5K' => 5*1024

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$verboseSize` | `string` | Verbose size representation. |

**Returns**

`int`: Actual size in bytes.

### getNowDate() <span id="method-getNowDate"></span>

```php
public function getNowDate(): string
```

Returns the now date if possible in string representation.

**Returns**

`string`: Now date.

### getResult() <span id="method-getResult"></span>

```php
public function getResult(): array|null
```

Return the check results.

**Returns**

`array|null`: Check results in format: array( 'summary' => array( 'total' => total number of checks, 'errors' => number of errors, 'warnings' => number of warnings, ), 'requirements' => array( array( ... 'error' => is there an error, 'warning' => is there a warning, ), ... ), )

### getServerInfo() <span id="method-getServerInfo"></span>

```php
public function getServerInfo(): string
```

Returns the server information.

**Returns**

`string`: Server information.

### normalizeRequirement() <span id="method-normalizeRequirement"></span>

```php
public function normalizeRequirement(array $requirement, int $requirementKey = 0): array
```

Normalizes requirement ensuring it has correct format.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$requirement` | `array` | Raw requirement. |
| `$requirementKey` | `int` | Requirement key in the list. |

**Returns**

`array`: Normalized requirement.

### render() <span id="method-render"></span>

```php
public function render(): mixed
```

Renders the requirements check result.

The output will vary depending is a script running from web or from console.

**Returns**

`mixed`

### renderViewFile() <span id="method-renderViewFile"></span>

```php
public function renderViewFile(string $_viewFile_, array|null $_data_ = \null, bool $_return_ = \false): string|null
```

Renders a view file.

This method includes the view file as a PHP script
and captures the display result if required.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$_viewFile_` | `string` | View file |
| `$_data_` | `array\|null` | Data to be extracted and made available to the view file |
| `$_return_` | `bool` | Whether the rendering result should be returned as a string |

**Returns**

`string|null`: The rendering result. Null if the rendering result is not required.

### usageError() <span id="method-usageError"></span>

```php
public function usageError(string $message): mixed
```

Displays a usage error.

This method will then terminate the execution of the current application.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$message` | `string` | The error message |

**Returns**

`mixed`