Skip to content

Commit

Permalink
Merge cdd70d3 into c2e2a0b
Browse files Browse the repository at this point in the history
  • Loading branch information
cambell-prince committed Nov 30, 2018
2 parents c2e2a0b + cdd70d3 commit 734784a
Show file tree
Hide file tree
Showing 18 changed files with 697 additions and 42 deletions.
154 changes: 154 additions & 0 deletions bin/anorm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#!/usr/bin/env php
<?php
namespace Anorm\Tools;

define ('VERSION', '0.1.0');

require_once (__DIR__ . '/../vendor/autoload.php');

use \cli\Arguments;

define ('HEADER', 'Anorm: Yet Another ORM CLI');

class App
{

/** @var string */
public $command;

/** @var array */
public $commandArgs;

/** @var array */
public $options;

public function __construct()
{
$arguments = new Arguments(array('strict' => false));
$arguments->addFlag(array('help', 'h'), 'Display this help');
$arguments->addFlag('version', 'Display the version');
$arguments->addFlag(array('force', 'f'), 'Force overwrite of files');
$arguments->addFlag(array('password', 'p'), 'Prompt for the database password');
$arguments->addOption(array('user', 'u'), array(
'default' => '',
'description' => 'Database username'
));
$arguments->addOption(array('classsuffix', 'c'), array(
'default' => 'Model',
'description' => 'Suffix for generated class names'
));
$arguments->addOption(array('models', 'm'), array(
'default' => 'src/Models/',
'description' => 'Models folder'
));
$arguments->addOption(array('namespace', 'n'), array(
'default' => 'App\Models',
'description' => 'Namespace for generated models'
));
$arguments->parse();
$this->options = $arguments;
$this->commandArgs = $arguments->getInvalidArguments();
$c = count($this->commandArgs);
if ($c >= 1) {
$this->command = array_shift($this->commandArgs);
}
}

public function run()
{
if ($this->options['help'])
{
$this->help();
return;
}
if ($this->options['version'])
{
$this->version();
return;
}
switch ($this->command)
{
case 'make':
$this->make();
break;

default:
echo $this->title();
printf("Error: Unknown command '%s', try 'help'\n", $this->command);
}

}

public function make()
{
echo $this->title();
// var_dump($this->commandArgs);
$database;
$table = '';
if (count($this->commandArgs) >= 2)
{
$table = $this->commandArgs[1];
}
if (count($this->commandArgs) >= 1)
{
$database = $this->commandArgs[0];
} else {
echo "Error: make command must have a database specified" . PHP_EOL;
return;
}

$password = '';
if ($this->options['password'])
{
$password = \cli\prompt("Password", false, ':', true); // hide
}
try
{
$pdo = new \PDO('mysql:host=localhost;dbname=' . $this->commandArgs[0], $this->options['user'], $password);
} catch (\PDOException $e)
{
echo 'Error: Database connection failed, ' . $e->getMessage() . PHP_EOL;
return;
}
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$tables = array($table);
foreach ($tables as $table)
{
$modelMakerOptions = new ModelMakerOptions();
$modelMakerOptions->classSuffix = $this->options['classsuffix'];
$modelMakerOptions->namespace = $this->options['namespace'];
$modelMaker = new \Anorm\Tools\ModelMaker($pdo, $table, $modelMakerOptions);
$filePath = $this->options['models'] . $modelMaker->fileName();
echo "Making model for $table in $filePath" . PHP_EOL;
$modelMaker->writeModelAsFile($filePath, array(), $this->options['force']);
}
}

public function help()
{
echo $this->title();
echo <<<'EOD'
Commands
make <database> [table]
Makes models for the given database table in the Models folder with Namespace.
EOD;
echo PHP_EOL;
echo $this->options->getHelpScreen();
echo PHP_EOL;
}

public function title()
{
return HEADER . " v" . VERSION . PHP_EOL;
}

public function version()
{
echo HEADER . PHP_EOL;
echo 'Version: ' . VERSION . PHP_EOL;
}

}

$app = new App();
$app->run();
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
"email": "cambell.prince@gmail.com"
}
],
"require": {},
"require": {
"wp-cli/php-cli-tools": "^0.11.10"
},
"autoload": {
"psr-4": {
"Anorm\\Tools\\": "tools/src/",
"Anorm\\": "src/"
}
}
},
"bin": ["bin/anorm.php"]
}
55 changes: 53 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions fred.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

$fred->task('test', function () use ($fred) {
// This included coveralls clover.xml code coverage by default.
include 'vendor/phpunit/phpunit/phpunit';
});

Expand All @@ -11,4 +12,13 @@
echo fread($f, 1024);
}
pclose($f);
});

$fred->task('test-quick', function () use ($fred) {
// exec('/usr/bin/env php vendor/bin/phpunit --coverage-html docs/code_coverage --coverage-text');
$f = popen('/usr/bin/env php vendor/bin/phpunit -c phpunit-no-coverage.xml', 'r');
while (!feof($f)) {
echo fread($f, 1024);
}
pclose($f);
});
12 changes: 12 additions & 0 deletions phpunit-no-coverage.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.3/phpunit.xsd"
>
<testsuites>
<testsuite name="Anorm Test Suite">
<directory>./test/anorm</directory>
<directory>./test/tools</directory>
</testsuite>
</testsuites>

</phpunit>
5 changes: 4 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
>
<testsuites>
<testsuite name="Anorm Test Suite">
<directory>./test</directory>
<directory>./test/anorm</directory>
<directory>./test/tools</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<directory suffix=".php">tools/src</directory>
<exclude>
<directory>vendor</directory>
<file>tools/src/anorm.php</file>
</exclude>
</whitelist>
</filter>
Expand Down
26 changes: 11 additions & 15 deletions src/DataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ class DataMapper

/** @var array<string, string> Map of property names to column names */
public $map;


/** @var string The property in the model that is used as the primary key */
public $modelPrimaryKey = 'id';

/** @var string Name of the table */
public $table;

Expand Down Expand Up @@ -92,8 +95,9 @@ public static function autoMap($c)
return $properties;
}

public function write(&$c, $key = 'id')
public function write(&$c)
{
$key = $this->modelPrimaryKey;
$set = '';
foreach ($this->map as $property => $field) {
if ($property == $key || $property[0] == '_') {
Expand All @@ -108,10 +112,6 @@ public function write(&$c, $key = 'id')
if ($c->$key === null) {
$sql = 'INSERT INTO `' . $this->table . '` SET ' . $set;
$result = $this->pdo->query($sql);
if ($result === false)
{
throw new SqlException($sql);
}
$c->$key = $this->pdo->lastInsertId();
} else {
$keyField = $this->map[$key];
Expand All @@ -121,11 +121,11 @@ public function write(&$c, $key = 'id')
}
}

public function read(&$c, $id, $key = 'id')
public function read(&$c, $id)
{
$keyField = $this->map[$key];
$databasePrimaryKey = $this->map[$this->modelPrimaryKey];
// TODO Could make the '*' explicit from the map
$sql = 'SELECT * FROM `' . $this->table . '` WHERE ' . $keyField . "='" . $id . "'";
$sql = 'SELECT * FROM `' . $this->table . '` WHERE ' . $databasePrimaryKey . "='" . $id . "'";
$result = $this->query($sql);
return $this->readRow($c, $result);
}
Expand Down Expand Up @@ -166,15 +166,11 @@ public function readArray(&$c, $data, $exclude = array())
return true;
}

public function delete($id, $key = 'id')
public function delete($id)
{
$keyField = $this->map[$key];
$keyField = $this->map[$this->modelPrimaryKey];
$sql = 'DELETE FROM `' . $this->table . '` WHERE ' . $keyField . "='" . $id . "'";
$result = $this->query($sql);
if ($result === false)
{
throw new SqlException($sql);
}
// This allows for imprecise deletes which may not be the best idea. CP 25 Nov 2018
return $result->rowCount() >= 1;
}
Expand Down
1 change: 1 addition & 0 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Model

public function __construct(\PDO $pdo, DataMapper $mapper)
{
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->_mapper = $mapper;
}

Expand Down
10 changes: 0 additions & 10 deletions src/SqlException.php

This file was deleted.

Loading

0 comments on commit 734784a

Please sign in to comment.