-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from fr3nch13/dev
Adding more utilities and testing.
- Loading branch information
Showing
33 changed files
with
2,219 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.