Skip to content

Commit

Permalink
Merge pull request #5 from fr3nch13/dev
Browse files Browse the repository at this point in the history
Adding more utilities and testing.
  • Loading branch information
fr3nch13 committed Feb 28, 2023
2 parents 878961b + f5ef1de commit 8987146
Show file tree
Hide file tree
Showing 33 changed files with 2,219 additions and 31 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"fr3nch13/composer-lock-parser": "~1.0"
},
"require-dev": {
"cakephp/migrations": "^3.7",
"fr3nch13/cakephp-pta": "dev-2.x-dev"
},
"autoload": {
Expand Down
50 changes: 50 additions & 0 deletions config/Migrations/00000000000001_InitialMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;

final class InitialMigration extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change(): void
{

$table = $this->table('books');
$table->addColumn('name', 'string', ['length' => '255'])
->addColumn('slug', 'string', ['length' => '255', 'null' => true])
->addColumn('student_id', 'integer', ['default' => null, 'null' => true])
->create();

$table = $this->table('courses');
$table->addColumn('name', 'string', ['length' => '255'])
->addColumn('name_other', 'string', ['length' => '255', 'null' => true])
->addColumn('slug', 'string', ['length' => '255', 'null' => true])
->addColumn('slug_other', 'string', ['length' => '255', 'null' => true])
->addColumn('updateme', 'string', ['length' => '255', 'null' => true])
->addColumn('available', 'boolean', ['default' => 1])
->addColumn('teachers_pet_id', 'integer', ['default' => null, 'null' => true])
->create();

$table = $this->table('students');
$table->addColumn('name', 'string', ['length' => '255'])
->addColumn('slug', 'string', ['length' => '255', 'null' => true])
->addColumn('updateme', 'string', ['length' => '255', 'null' => true])
->create();

$table = $this->table('courses_students');
$table->addColumn('course_id', 'integer')
->addColumn('student_id', 'integer')
->addColumn('grade', 'integer')
->create();
}
}
19 changes: 0 additions & 19 deletions src/Exception/Exception.php

This file was deleted.

4 changes: 1 addition & 3 deletions src/Exception/InvalidCharException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@

namespace Fr3nch13\Utilities\Exception;

use Cake\Core\Exception\CakeException;

/**
* Invalid Character Exception
*
* Throw when a character is invalid.
*/
class InvalidCharException extends CakeException
class InvalidCharException extends UtilitieException
{
}
23 changes: 23 additions & 0 deletions src/Exception/MissingMethodException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

/**
* InvalidCharException
*/

namespace Fr3nch13\Utilities\Exception;

/**
* Invalid Character Exception
*
* Throw when a character is invalid.
*/
class MissingMethodException extends UtilitieException
{
/**
* Template string that has attributes sprintf()'ed into it.
*
* @var string
*/
protected $_messageTemplate = 'Missing the `%s::%s()` method.%s';
}
25 changes: 25 additions & 0 deletions src/Exception/UtilitieException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

/**
* Exception
*/

namespace Fr3nch13\Utilities\Exception;

use Cake\Core\Exception\CakeException;

/**
* Exception
*
* Throw when a config file is missing.
*/
class UtilitieException extends CakeException
{
/**
* Default exception code
*
* @var int
*/
protected $_defaultCode = 500;
}
86 changes: 86 additions & 0 deletions src/Lib/Memory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);

/**
* MemoryBehavior
*/

namespace Fr3nch13\Utilities\Lib;

/**
* Tracks Memory and time usage, mainly for cron jobs.
*/
class Memory
{
/**
* Default config settings.
*
* @var array<string, mixed>
*/
protected $_defaultConfig = [];

/**
* Trackes the start times.
* Format is ['(time_key)' => (unix timestamp)].
*
* @var array<string, int>
*/
protected $_startTimes = [];

/**
* Trackes the end times.
* Format is ['(time_key)' => (unix timestamp)].
*
* @var array<string, int>
*/
protected $_endTimes = [];

/**
* Tracks the highest recorded memory usage from when memoryUsage was called.
*
* @var float
*/
protected $memUsageHighest = 0;

/**
* Reports the memory usage at the time it is called.
*
* @param bool $nice If we should return the bytes (false), of the calculated amount in a nice format (true).
* @param float|null $mem_usage The memory number to be made nice.
* @return string the memory usage stat.
*/
public function usage(bool $nice = true, ?float $mem_usage = null): string
{
if (!$mem_usage) {
$mem_usage = memory_get_usage();
}
// track the highest usage.
if ($this->memUsageHighest < $mem_usage) {
$this->memUsageHighest = $mem_usage;
}
if ($nice) {
if ($mem_usage < 1024) {
$mem_usage = $mem_usage . ' B';
} elseif ($mem_usage < 1048576) {
$mem_usage = round($mem_usage / 1024, 2) . ' KB';
} elseif ($mem_usage < 1073741824) {
$mem_usage = round($mem_usage / 1048576, 2) . ' MB';
} else {
$mem_usage = round($mem_usage / 1073741824, 2) . ' GB';
}
}

return strval($mem_usage);
}

/**
* Reports the highest memory usage.
*
* @param bool $nice If we should return the bytes (false), of the calculated amount in a nice format (true).
* @return string the highest memory usage stat.
*/
public function usageHighest($nice = true): string
{
return $this->usage($nice, $this->memUsageHighest);
}
}
67 changes: 67 additions & 0 deletions src/Model/Behavior/MemoryBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);

/**
* MemoryBehavior
*/

namespace Fr3nch13\Utilities\Model\Behavior;

use Cake\ORM\Behavior;
use Fr3nch13\Utilities\Lib\Memory;

/**
* Memory Behavior
*
* Tracks Memory and time usage, mainly for cron jobs.
* This is just a wrapper around the Lib\Memory class.
* This may not even be needed.
*/
class MemoryBehavior extends Behavior
{
/**
* The memory tracking object.
*
* @var null|\Fr3nch13\Utilities\Lib\Memory;
*/
protected $Memory = null;

/**
* Reports the memory usage at the time it is called.
*
* @param bool $nice If we should return the bytes (false), of the calculated amount in a nice format (true).
* @param float|null $mem_usage The memory number to be made nice.
* @return string the memory usage stat.
*/
public function memoryUsage(bool $nice = true, ?float $mem_usage = null): string
{
$this->ensureMemory();

return $this->Memory->usage($nice, $mem_usage);
}

/**
* Reports the highest memory usage.
*
* @param bool $nice If we should return the bytes (false), of the calculated amount in a nice format (true).
* @return string the highest memory usage stat.
*/
public function memoryUsageHighest($nice = true): string
{
$this->ensureMemory();

return $this->Memory->usageHighest($nice);
}

/**
* Makes sure there is a Memory object created.
*
* @return void
*/
public function ensureMemory(): void
{
if (!$this->Memory) {
$this->Memory = new Memory();
}
}
}

0 comments on commit 8987146

Please sign in to comment.