PHPackages                             monkeyscloud/monkeyslegion-core - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. monkeyscloud/monkeyslegion-core

ActiveLibrary[Framework](/categories/framework)

monkeyscloud/monkeyslegion-core
===============================

Core runtime (kernel, events, helpers) for the MonkeysLegion PHP framework.

1.0.7(1mo ago)01.3k↑250%11MITPHPPHP ^8.4

Since Jul 23Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/MonkeysCloud/MonkeysLegion-Core)[ Packagist](https://packagist.org/packages/monkeyscloud/monkeyslegion-core)[ RSS](/packages/monkeyscloud-monkeyslegion-core/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (22)Versions (10)Used By (11)

MonkeysLegion Core
==================

[](#monkeyslegion-core)

[![PHP Version](https://camo.githubusercontent.com/504ead6a583c68d8d62d7bfceed24e569ca613d7a36bed380281b3455b5c7b31/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e342d626c7565)](https://www.php.net/)[![License](https://camo.githubusercontent.com/f8df3091bbe1149f398a5369b2c39e896766f9f6efba3477c63e9b4aa940ef14/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e)](LICENSE)

Core runtime package for the **MonkeysLegion PHP Framework**, providing essential components including routing, middleware, dependency injection integration, logging, and helper utilities.

Overview
--------

[](#overview)

MonkeysLegion-Core is a foundational library that provides:

- **Route Loading**: Automatic controller discovery and route registration
- **CORS Middleware**: Advanced PSR-15 CORS handling with flexible origin matching
- **Smart Logging**: Environment-aware logging with PSR-3 compliance
- **Helper Functions**: Common utilities for path resolution and debugging
- **Service Provider Interface**: Standardized component registration

Requirements
------------

[](#requirements)

- PHP 8.4 or higher
- PSR-7 (HTTP Message)
- PSR-11 (Container)
- PSR-15 (HTTP Server Middleware)
- PSR-17 (HTTP Factories)

Installation
------------

[](#installation)

```
composer require monkeyscloud/monkeyslegion-core
```

Components
----------

[](#components)

### 1. Route Loader

[](#1-route-loader)

The [`RouteLoader`](src/Routing/RouteLoader.php) automatically scans your controller directory and registers routes defined via attributes.

#### Usage

[](#usage)

```
use MonkeysLegion\Core\Routing\RouteLoader;

$loader = new RouteLoader(
    router: $router,
    container: $container,
    controllerDir: base_path('app/Controller'),
    controllerNS: 'App\\Controller'
);

$loader->loadControllers();
```

**Features:**

- Recursive directory scanning
- Automatic class instantiation via DI container
- Skips abstract classes
- Integrates with `monkeyscloud/monkeyslegion-router`

---

### 2. CORS Middleware

[](#2-cors-middleware)

The [`CorsMiddleware`](src/Middleware/CorsMiddleware.php) is a fully-featured PSR-15 middleware for handling Cross-Origin Resource Sharing.

#### Usage

[](#usage-1)

```
use MonkeysLegion\Core\Middleware\CorsMiddleware;

$cors = new CorsMiddleware(
    allowOrigin: ['https://example.com', '/^https:\/\/.*\.example\.com$/'],
    allowMethods: ['GET', 'POST', 'PATCH', 'DELETE', 'OPTIONS'],
    allowHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
    exposeHeaders: ['X-Total-Count'],
    allowCredentials: true,
    maxAge: 86400,
    responseFactory: $responseFactory
);
```

**Features:**

- **Origin Matching**: Supports wildcards (`*`), exact strings, or PCRE patterns
- **Pre-flight Handling**: Automatic OPTIONS request handling
- **Credentials Support**: Configurable `Access-Control-Allow-Credentials`
- **Cache Safety**: Adds `Vary: Origin` header
- **Error Handling**: Catches exceptions and returns JSON error responses

---

### 3. Helper Functions

[](#3-helper-functions)

Defined in [`src/Support/helpers.php`](src/Support/helpers.php):

#### `base_path(string $path = ''): string`

[](#base_pathstring-path---string)

Returns an absolute path relative to the project root.

```
base_path();                    // → /full/path/to/project
base_path('var/migrations');    // → /full/path/to/project/var/migrations
base_path('config/app.php');    // → /full/path/to/project/config/app.php
```

**Configuration:**

- Define `ML_BASE_PATH` constant to set the project root
- Falls back to `dirname(__DIR__, 4)` for testing environments

#### `dd(mixed ...$args): void`

[](#ddmixed-args-void)

Dump variables and terminate script execution.

```
dd($user, $order);  // Dumps both variables and exits
```

**Features:**

- CLI-aware output (plain text vs HTML)
- XSS-safe HTML output for web contexts
- Handles arrays, objects, scalars, and null values
- Exits with status code 1

---

### 4. Provider Interface

[](#4-provider-interface)

The [`ProviderInterface`](src/Provider/ProviderInterface.php) standardizes how components register themselves with the framework.

```
interface ProviderInterface
{
    public static function register(string $root, ContainerBuilder $c): void;
    public static function setLogger(FrameworkLoggerInterface $logger): void;
}
```

Implement this interface in your service providers for consistent component registration.

---

Architecture
------------

[](#architecture)

### PSR Compliance

[](#psr-compliance)

This package strictly adheres to PHP-FIG standards:

- **PSR-7**: HTTP Message Interface (used in [`CorsMiddleware`](src/Middleware/CorsMiddleware.php))
- **PSR-11**: Container Interface (used in [`RouteLoader`](src/Routing/RouteLoader.php))
- **PSR-15**: HTTP Server Request Handlers ([`CorsMiddleware`](src/Middleware/CorsMiddleware.php))
- **PSR-17**: HTTP Factories ([`CorsMiddleware`](src/Middleware/CorsMiddleware.php))

### Type Safety

[](#type-safety)

- Strict type declarations (`declare(strict_types=1)`)
- Full PHP 8.4 type hints
- PHPStan level max compliance

---

Development
-----------

[](#development)

### Static Analysis

[](#static-analysis)

```
vendor/bin/phpstan analyse
```

Configuration: [phpstan.neon](phpstan.neon)

### Code Quality Standards

[](#code-quality-standards)

- **Strict Types**: All files use `declare(strict_types=1)`
- **Final Classes**: Components use `final` to prevent inheritance where appropriate
- **Type Hints**: Full parameter and return type declarations
- **PHPDoc**: Comprehensive documentation blocks

---

Integration Example
-------------------

[](#integration-example)

```
use MonkeysLegion\Core\Routing\RouteLoader;
use MonkeysLegion\Core\Middleware\CorsMiddleware;
use MonkeysLegion\Core\Logger\MonkeyLogger;

// Set up logger
$logger = new MonkeyLogger($psrLogger, $_ENV['APP_ENV']);

// Configure CORS
$cors = new CorsMiddleware(
    allowOrigin: ['https://app.example.com'],
    allowMethods: ['GET', 'POST', 'PATCH', 'DELETE'],
    allowCredentials: true
);

// Load routes
$routeLoader = new RouteLoader(
    $router,
    $container,
    base_path('app/Controller'),
    'App\\Controller'
);
$routeLoader->loadControllers();

// Add middleware to pipeline
$middleware->add($cors);
```

---

Dependencies
------------

[](#dependencies)

### Required

[](#required)

- `php`: ^8.4
- `psr/container`: ^2.0
- `psr/log`: ^3.0
- `psr/http-message`: ^2.0
- `psr/http-server-handler`: ^1.0
- `psr/http-server-middleware`: ^1.0
- `psr/http-factory`: ^1.1
- `monkeyscloud/monkeyslegion-http`: ^1.0
- `monkeyscloud/monkeyslegion-router`: ^1.0
- `monkeyscloud/monkeyslegion-di`: ^1.0

### Development

[](#development-1)

- `phpstan/phpstan`: ^2.1

---

License
-------

[](#license)

MIT License. See LICENSE file for details.

Contributing
------------

[](#contributing)

Contributions are welcome! Please ensure:

1. Code follows PSR-12 coding standards
2. All code passes PHPStan level max
3. New features include appropriate documentation
4. Type hints are comprehensive

Support
-------

[](#support)

For issues, questions, or contributions, please visit the project repository.

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance79

Regular maintenance activity

Popularity21

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 55% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~34 days

Total

8

Last Release

57d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/51e4df19377776baa8eafb605d9e7d2374b855c686f552c20d6856e94e3597c3?d=identicon)[yorchperaza](/maintainers/yorchperaza)

---

Top Contributors

[![yorchperaza](https://avatars.githubusercontent.com/u/2913369?v=4)](https://github.com/yorchperaza "yorchperaza (11 commits)")[![Amanar-Marouane](https://avatars.githubusercontent.com/u/155680356?v=4)](https://github.com/Amanar-Marouane "Amanar-Marouane (9 commits)")

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/monkeyscloud-monkeyslegion-core/health.svg)

```
[![Health](https://phpackages.com/badges/monkeyscloud-monkeyslegion-core/health.svg)](https://phpackages.com/packages/monkeyscloud-monkeyslegion-core)
```

###  Alternatives

[cakephp/cakephp

The CakePHP framework

8.8k18.5M1.6k](/packages/cakephp-cakephp)[neos/flow

Flow Application Framework

862.0M451](/packages/neos-flow)[thecodingmachine/graphqlite

Write your GraphQL queries in simple to write controllers (using webonyx/graphql-php).

5723.1M30](/packages/thecodingmachine-graphqlite)[neos/flow-development-collection

Flow packages in a joined repository for pull requests.

144179.3k3](/packages/neos-flow-development-collection)[windwalker/framework

The next generation PHP framework.

25639.1k1](/packages/windwalker-framework)[yiisoft/yii-middleware

Yii Middleware

21151.3k1](/packages/yiisoft-yii-middleware)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
