Skip to content

Commit

Permalink
Merge pull request #18 from stevegrunwell/namespacing
Browse files Browse the repository at this point in the history
Namespace the constants by default
  • Loading branch information
stevegrunwell authored Aug 20, 2024
2 parents 796f421 + 5297819 commit 5f1c96e
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 147 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
coverage: none

- name: Install Composer dependencies
uses: ramsey/composer-install@v2
uses: ramsey/composer-install@v3

- name: Check coding standards
run: make standards
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
coverage: none

- name: Install Composer dependencies
uses: ramsey/composer-install@v2
uses: ramsey/composer-install@v3

- name: Run test suite
run: make unit-tests
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"squizlabs/php_codesniffer": "^3.10"
},
"autoload": {
"files": ["constants.php"]
"files": ["src/Constants.php"]
},
"autoload-dev": {
"psr-4": {
Expand Down
114 changes: 0 additions & 114 deletions constants.php

This file was deleted.

13 changes: 10 additions & 3 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
<arg value="sp"/>
<arg name="extensions" value="php"/>

<file>constants.php</file>
<file>src</file>
<file>tests</file>

<rule ref="PSR12"/>

<!-- Test for compatibility issues with older versions of PHP.-->
<rule ref="PHPCompatibility"/>
<!-- Test (non-dev) code for compatibility issues with older versions of PHP.-->
<rule ref="PHPCompatibility">
<exclude-pattern>tests</exclude-pattern>
</rule>
<config name="testVersion" value="7.0-"/>

<!-- The compatibility layer is special, we don't care that it defines + executes code. -->
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<exclude-pattern>src/GlobalAliases.php</exclude-pattern>
</rule>
</ruleset>
118 changes: 118 additions & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

/**
* Defines useful time-based constants.
*
* @package SteveGrunwell\TimeConstants
*/

declare(strict_types=1);

namespace TimeConstants;

/**
* Time based in seconds.
*
* This is commonly used for things like cache expirations or task scheduling, and are heavily
* inspired by WordPress' time constants.
*
* @link https://codex.wordpress.org/Easier_Expression_of_Time_Constants
*/

/* One second. */
if (!defined(__NAMESPACE__ . '\\ONE_SECOND')) {
define(__NAMESPACE__ . '\\ONE_SECOND', 1);
}

/* One minute = 60 seconds. */
if (!defined(__NAMESPACE__ . '\\MINUTE_IN_SECONDS')) {
define(__NAMESPACE__ . '\\MINUTE_IN_SECONDS', 60);
}

/* One hour = 60 minutes. */
if (!defined(__NAMESPACE__ . '\\HOUR_IN_SECONDS')) {
define(__NAMESPACE__ . '\\HOUR_IN_SECONDS', 60 * MINUTE_IN_SECONDS);
}

/* One day = 24 hours. */
if (!defined(__NAMESPACE__ . '\\DAY_IN_SECONDS')) {
define(__NAMESPACE__ . '\\DAY_IN_SECONDS', 24 * HOUR_IN_SECONDS);
}

/* One week = 7 days. */
if (!defined(__NAMESPACE__ . '\\WEEK_IN_SECONDS')) {
define(__NAMESPACE__ . '\\WEEK_IN_SECONDS', 7 * DAY_IN_SECONDS);
}

/* For common usage, assume one month = 30 days. */
if (!defined(__NAMESPACE__ . '\\MONTH_IN_SECONDS')) {
define(__NAMESPACE__ . '\\MONTH_IN_SECONDS', 30 * DAY_IN_SECONDS);
}

/* For common usage, assume one year = 365 days. */
if (!defined(__NAMESPACE__ . '\\YEAR_IN_SECONDS')) {
define(__NAMESPACE__ . '\\YEAR_IN_SECONDS', 365 * DAY_IN_SECONDS);
}

/**
* Time based in minutes.
*
* For platforms that measure time based in minutes (such as Laravel's Cache facade), these
* constants provide similar functionality.
*/

/* One minute. */
if (!defined(__NAMESPACE__ . '\\ONE_MINUTE')) {
define(__NAMESPACE__ . '\\ONE_MINUTE', 1);
}

/* One hour = 60 minutes. */
if (!defined(__NAMESPACE__ . '\\HOUR_IN_MINUTES')) {
define(__NAMESPACE__ . '\\HOUR_IN_MINUTES', 60);
}

/* One day = 24 hours. */
if (!defined(__NAMESPACE__ . '\\DAY_IN_MINUTES')) {
define(__NAMESPACE__ . '\\DAY_IN_MINUTES', 24 * HOUR_IN_MINUTES);
}

/* One week = 7 days. */
if (!defined(__NAMESPACE__ . '\\WEEK_IN_MINUTES')) {
define(__NAMESPACE__ . '\\WEEK_IN_MINUTES', 7 * DAY_IN_MINUTES);
}

/* For common usage, assume one month = 30 days. */
if (!defined(__NAMESPACE__ . '\\MONTH_IN_MINUTES')) {
define(__NAMESPACE__ . '\\MONTH_IN_MINUTES', 30 * DAY_IN_MINUTES);
}

/* For common usage, assume one year = 365 days. */
if (!defined(__NAMESPACE__ . '\\YEAR_IN_MINUTES')) {
define(__NAMESPACE__ . '\\YEAR_IN_MINUTES', 365 * DAY_IN_MINUTES);
}

/**
* Common multipliers.
*
* These are useful when dealing with timing and things like cache expirations.
*/

/* A millisecond is 1/1000 of a second. */
if (!defined(__NAMESPACE__ . '\\MILLISECONDS_PER_SECOND')) {
define(__NAMESPACE__ . '\\MILLISECONDS_PER_SECOND', 1000);
}

/* A microsecond is one millionth of a second. */
if (!defined(__NAMESPACE__ . '\\MICROSECONDS_PER_SECOND')) {
define(__NAMESPACE__ . '\\MICROSECONDS_PER_SECOND', 1000000);
}

/* A nanosecond is one billionth of a second. */
if (!defined(__NAMESPACE__ . '\\NANOSECONDS_PER_SECOND')) {
define(__NAMESPACE__ . '\\NANOSECONDS_PER_SECOND', 1000000000);
}

/* A picosecond is one trillionth of a second. */
if (!defined(__NAMESPACE__ . '\\PICOSECONDS_PER_SECOND')) {
define(__NAMESPACE__ . '\\PICOSECONDS_PER_SECOND', 1000000000000);
}
43 changes: 43 additions & 0 deletions src/GlobalAliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Global aliases for time constants.
*
* This is meant to help bridge the gap between versions 1.x and 2.x of the library.
*
* If your application or library has references to the old, globally-namespaced constants, you may
* either load this file via `autoload.files` in your `composer.json` file or by requiring the file
* directly in your code.
*
* @package SteveGrunwell\TimeConstants
*/

declare(strict_types=1);

$time_constants = [
'ONE_SECOND',
'MINUTE_IN_SECONDS',
'HOUR_IN_SECONDS',
'DAY_IN_SECONDS',
'WEEK_IN_SECONDS',
'MONTH_IN_SECONDS',
'YEAR_IN_SECONDS',
'ONE_MINUTE',
'HOUR_IN_MINUTES',
'DAY_IN_MINUTES',
'WEEK_IN_MINUTES',
'MONTH_IN_MINUTES',
'YEAR_IN_MINUTES',
'MILLISECONDS_PER_SECOND',
'MICROSECONDS_PER_SECOND',
'NANOSECONDS_PER_SECOND',
'PICOSECONDS_PER_SECOND',
];

foreach ($time_constants as $constant) {
if (!defined($constant)) {
define($constant, constant('TimeConstants\\' . $constant));
}
}

unset($time_constants);
Loading

0 comments on commit 5f1c96e

Please sign in to comment.