Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit bed46d6

Browse files
committed
initial commit
0 parents  commit bed46d6

19 files changed

+804
-0
lines changed

.github/workflows/php.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: PHP Composer
2+
3+
on:
4+
push:
5+
branches:
6+
- '**'
7+
pull_request:
8+
branches:
9+
- '**'
10+
11+
permissions:
12+
contents: read
13+
14+
jobs:
15+
build:
16+
17+
runs-on: ubuntu-latest
18+
19+
strategy:
20+
fail-fast: false
21+
matrix:
22+
php: [ '8.1', '8.2' ]
23+
24+
steps:
25+
- uses: actions/checkout@v3
26+
27+
- name: Validate composer.json and composer.lock
28+
run: composer validate --strict
29+
30+
- name: Setup PHP
31+
uses: shivammathur/setup-php@v2
32+
with:
33+
php-version: ${{ matrix.php }}
34+
coverage: xdebug
35+
ini-values: xdebug.mode=develop,debug,coverage,
36+
37+
- name: Install dependencies
38+
run: composer install --prefer-dist --no-progress
39+
40+
- name: PHP-CS-Fixer
41+
run: composer fix:dry
42+
43+
- name: PHPUnit
44+
run: composer test

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### PHP-CS-Fixer
2+
.php-cs-fixer.cache
3+
.php-cs-fixer.php
4+
5+
### Composer
6+
composer.phar
7+
/vendor/
8+
composer.lock
9+
10+
### JetBrains IDEs
11+
/.idea/
12+
13+
### VSCode
14+
/.vscode/
15+
16+
### PHPUnit
17+
/.phpunit.cache/

.php-cs-fixer.dist.php

Lines changed: 257 additions & 0 deletions
Large diffs are not rendered by default.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Totally Not Another PHP Framework
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# PackageTemplate
2+
3+
A template repository for making composer packages
4+
5+
## Requirements
6+
7+
- PHP 8.1+
8+
9+
## What's Included
10+
11+
- [Composer Normalize](https://github.com/ergebnis/composer-normalize)
12+
- [Faker](https://github.com/fakerphp/faker)
13+
- [PHP-CS-Fixer](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer)
14+
- [JetBrains Attributes](https://github.com/jetbrains/phpstorm-attributes)
15+
- [PHPUnit](https://phpunit.de)
16+
- [Roave Security Advisories](https://github.com/Roave/SecurityAdvisories)
17+
- [Symfony VarDumper](https://github.com/symfony/var-dumper)
18+
- [Composer Git Hooks](https://github.com/xheaven/composer-git-hooks)
19+
20+
- GitHub Actions
21+
- Composer Scripts
22+
- `composer test` - Run PHPUnit tests
23+
- `composer test:coverage` - Run PHPUnit tests with coverage
24+
- `composer fix:dry` - Run PHP-CS-Fixer in Dry-Run mode
25+
- `composer fix` - Run PHP-CS-Fixer and fix errors
26+
27+
## Installation
28+
29+
```bash
30+
composer create-project tnapf/package <package>
31+
```
32+
33+
## Setup GitHub Repository
34+
35+
1. Run `git init` to initialize a new git repository
36+
2. Run `git add .` to add all files to the repository
37+
3. Run `git commit -m "initial commit"` to commit the files
38+
4. Run `git branch -M main` to rename the current branch to `main`
39+
5. Create a new repository on GitHub
40+
6. Run `git remote add origin https://github.com/tnapf/<package>.git` to add the new repository as remote
41+
7. Run `git push -u origin main` to push the files to the new repository

composer.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "cavephp/cavephp",
3+
"description": "The core the CavePHP",
4+
"license": "MIT",
5+
"type": "library",
6+
"authors": [
7+
{
8+
"name": "Robert Snedeker",
9+
"email": "[email protected]"
10+
},
11+
{
12+
"name": "Diego Casillas",
13+
"email": "[email protected]"
14+
}
15+
],
16+
"require": {
17+
"php": ">=8.2",
18+
"react/socket": "^1.14"
19+
},
20+
"require-dev": {
21+
"friendsofphp/php-cs-fixer": "^3.16",
22+
"phpunit/phpunit": "^10.1"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"CavePHP\\": "src/"
27+
},
28+
"files": [
29+
"src/functions.php"
30+
]
31+
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"CavePHP\\Tests\\": "tests/"
35+
}
36+
},
37+
"scripts": {
38+
"fix": "php-cs-fixer fix --using-cache=no",
39+
"fix:dry": "php-cs-fixer fix --using-cache=no --diff --dry-run",
40+
"test": "phpunit",
41+
"test:coverage": "phpunit --coverage-html .phpunit.cache/cov-html"
42+
},
43+
"config": {
44+
"allow-plugins": {
45+
"ergebnis/composer-normalize": false
46+
}
47+
}
48+
}

index.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
use CavePHP\Server\Logger\Level;
4+
use CavePHP\Server\Server;
5+
6+
require_once __DIR__ . '/vendor/autoload.php';
7+
8+
$server = new Server();
9+
10+
$server->on(Server::READY, function () use ($server) {
11+
$server->logger->log(Level::INFO, 'Server is ready!');
12+
});
13+
14+
$server->start();

phpunit.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
4+
beStrictAboutOutputDuringTests="true" failOnRisky="true" failOnWarning="true"
5+
cacheDirectory=".phpunit.cache">
6+
<testsuites>
7+
<testsuite name="default">
8+
<directory>tests</directory>
9+
</testsuite>
10+
</testsuites>
11+
<coverage />
12+
<source>
13+
<include>
14+
<directory suffix=".php">src</directory>
15+
</include>
16+
</source>
17+
</phpunit>

src/Exceptions/CavePHPException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace CavePHP\Exceptions;
4+
5+
use Exception;
6+
7+
class CavePHPException extends Exception {
8+
9+
}

src/Pools/Pool.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace CavePHP\Pools;
4+
5+
use CavePHP\Exceptions\CavePHPException;
6+
use Countable;
7+
8+
abstract class Pool implements Countable {
9+
private array $items = [];
10+
11+
abstract public function isInstance(object $item): bool;
12+
13+
public function __construct(array $items = []) {
14+
$this->fill($items);
15+
}
16+
17+
public function fill(array $items): void
18+
{
19+
foreach ($items as $item) {
20+
if (!is_object($item) || !$this->isInstance($item)) {
21+
throw new CavePHPException("This object is not supported by this collection");
22+
}
23+
}
24+
25+
$this->items = [...$this->items, ...$items];
26+
}
27+
28+
public function add(mixed $item): void
29+
{
30+
$this->fill([$item]);
31+
}
32+
33+
public function remove(mixed $element): void
34+
{
35+
$this->items = array_filter($this->items, static fn ($item) => $item !== $element);
36+
}
37+
38+
public function filter(callable $callback): static
39+
{
40+
$that = clone $this;
41+
42+
$that->loop(static function (object $item) use ($that) {
43+
$that->remove($item);
44+
});
45+
46+
return $that;
47+
}
48+
49+
public function loop(callable $callback): void
50+
{
51+
foreach ($this->items as $item) {
52+
$callback($item);
53+
}
54+
}
55+
56+
public function find(callable $callback): ?object
57+
{
58+
foreach ($this->items as $item) {
59+
if ($callback($item)) {
60+
return $item;
61+
}
62+
}
63+
64+
return null;
65+
}
66+
67+
public function reduce(callable $callback, mixed $initial = null): mixed
68+
{
69+
$reduced = $initial;
70+
71+
$this->loop(static fn ($reduced, $item) => $callback($reduced, $item));
72+
73+
return $reduced;
74+
}
75+
76+
public function count(): int
77+
{
78+
return count($this->items);
79+
}
80+
}

src/Server/Client.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace CavePHP\Server;
4+
5+
use CavePHP\Utilities\EventEmitter;
6+
use React\Socket\ConnectionInterface;
7+
8+
class Client extends EventEmitter
9+
{
10+
public const RAW_DATA = 'raw_data';
11+
public const PACKET = 'packet';
12+
13+
public function __construct(
14+
protected readonly ConnectionInterface $connection
15+
) {
16+
$this->setupEvents();
17+
}
18+
19+
public function getIp(): string
20+
{
21+
return $this->connection->getRemoteAddress();
22+
}
23+
24+
private function setupEvents(): void
25+
{
26+
$this->connection->on('data', function ($data) {
27+
$this->emit('data', [$data]);
28+
});
29+
}
30+
}

src/Server/Logger/Color.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace CavePHP\Server\Logger;
4+
5+
final class Color
6+
{
7+
public const RED = "#ff695e";
8+
public const RED_INVERTED = "#db2828";
9+
public const ORANGE = "#ff851b";
10+
public const ORANGE_INVERTED = "#f2711c";
11+
public const YELLOW = "#ffe21f";
12+
public const YELLOW_INVERTED = "#fbbd08";
13+
public const OLIVE = "#d9e778";
14+
public const OLIVE_INVERTED = "#b5cc18";
15+
public const GREEN = "#2ecc40";
16+
public const GREEN_INVERTED = "#21ba45";
17+
public const TEAL = "#6dffff";
18+
public const TEAL_INVERTED = "#00b5ad";
19+
public const BLUE = "#54c8ff";
20+
public const BLUE_INVERTED = "#2185d0";
21+
public const VIOLET = "#a291fb";
22+
public const VIOLET_INVERTED = "#6435c9";
23+
public const PURPLE = "#dc73ff";
24+
public const PURPLE_INVERTED = "#a333c8";
25+
public const PINK = "#ff8edf";
26+
public const PINK_INVERTED = "#e03997";
27+
public const BROWN = "#d67c1c";
28+
public const BROWN_INVERTED = "#a5673f";
29+
public const GREY = "#dcddde";
30+
public const GREY_INVERTED = "#767676";
31+
public const BLACK = "#545454";
32+
public const BLACK_INVERTED = "#1b1c1d";
33+
}

src/Server/Logger/Level.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace CavePHP\Server\Logger;
4+
5+
use CavePHP\Server\Logger\Color;
6+
use Stringable;
7+
8+
enum Level: string
9+
{
10+
case ERROR = Color::RED_INVERTED;
11+
case WARNING = Color::YELLOW_INVERTED;
12+
case INFO = Color::BLUE_INVERTED;
13+
case DEBUG = Color::BLACK;
14+
}

0 commit comments

Comments
 (0)