Skip to content

Commit

Permalink
still trying HUGE redo NOT FINISHED
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Jan 17, 2017
1 parent 076f91f commit 95bd315
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 178 deletions.
21 changes: 5 additions & 16 deletions src/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface
*/
public $joinWith = [];

/**
* @var array options for search
*/
public $options = [];

/**
* Constructor.
*
* @param string $modelClass the model class associated with this query
* @param array $config configurations to be applied to the newly created query object
*/
Expand All @@ -65,10 +59,8 @@ public function init()

/**
* Creates a DB command that can be used to execute this query.
*
* @param Connection $db the DB connection used to create the DB command.
* If null, the DB connection returned by [[modelClass]] will be used.
*
* If null, the DB connection returned by [[modelClass]] will be used.
* @return Command the created DB command instance
*/
public function createCommand($db = null)
Expand All @@ -95,16 +87,12 @@ public function createCommand($db = null)

/* @var $modelClass ActiveRecord */
$modelClass = $this->modelClass;

if ($db === null) {
$db = $modelClass::getDb();
}

if ($this->type === null) {
$this->type = $modelClass::type();
}
if ($this->index === null) {
$this->index = $modelClass::index();
$this->type = $modelClass::type();
if ($this->from === null) {
$this->from = $modelClass::from();
}

$commandConfig = $db->getQueryBuilder()->build($this);
Expand All @@ -113,6 +101,7 @@ public function createCommand($db = null)
}

/**
* Prepares query for use. See NOTE.
* @return static
*/
public function prepare()
Expand Down
20 changes: 5 additions & 15 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public static function find($options = [])
{
$config = [
'class' => ActiveQuery::class,
'db' => $this->getDb(),
'options' => $options,
];

Expand Down Expand Up @@ -88,7 +87,7 @@ public static function get($primaryKey = null, $options = [])
return null;
}
$command = static::getDb()->createCommand();
$result = $command->get(static::type(), $primaryKey, $options);
$result = $command->get(static::from(), $primaryKey, $options);

if ($result) {
$model = static::instantiate($result);
Expand Down Expand Up @@ -220,12 +219,6 @@ public static function joinIndex()
* For example, by creating a record based on the value of a column,
* you may implement the so-called single-table inheritance mapping.
*
* @param array $row row data to be populated into the record.
* This array consists of the following keys:
* - `_source`: refers to the attributes of the record.
* - `_type`: the type this record is stored in.
* - `_index`: the index this record is stored in.
*
* @return static the newly created active record
*/
public static function instantiate($row)
Expand All @@ -234,9 +227,9 @@ public static function instantiate($row)
}

/**
* @return string the name of the type of this record
* @return string the name of the entity of this record
*/
public static function type()
public static function from()
{
return Inflector::camel2id(StringHelper::basename(get_called_class()), '-');
}
Expand Down Expand Up @@ -348,21 +341,18 @@ protected function perform($defaultScenario, $data, $bulk = false)
*/
public static function performAction($action, $options = [], $bulk = false)
{
$action = ($bulk === true ? static::index() : static::type()) . $action;
$action = ($bulk === true ? static::index() : static::from()) . $action;
$result = static::getDb()->createCommand()->perform($action, $options);

return $result;
}

/**
* Creates command name from the current scenario name.
*
* @param string $default
* @param bool $bulk
*
* @throws InvalidConfigException
* @throws NotSupportedException
*
* @return string
*/
public function getScenarioCommand($default = '', $bulk = false)
Expand Down Expand Up @@ -395,7 +385,7 @@ public function getScenarioCommand($default = '', $bulk = false)
if (is_array($result)) {
return implode('', $result);
} else {
return static::type() . ($bulk ? 's' : '') . $result;
return ($bulk ? 's' : '') . $result;
}
}

Expand Down
133 changes: 39 additions & 94 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,148 +10,93 @@

namespace hiqdev\hiart;

use yii\base\Component;
use yii\helpers\ArrayHelper;
use yii\helpers\Inflector;
use yii\helpers\Json;
use Psr\Http\Message\RequestInterface;

/**
* The Command class implements execution of query.
* The Command class implements execution of request.
*/
class Command extends Component
class Command extends \yii\base\Component
{
/**
* @var Connection
*/
public $db;

/**
* @var Query Query object
* @var RequestInterface request object
*/
public $query;
protected $_request;

/**
* @var string request method e.g. POST
*/
protected $method;

/**
* @var string request url, without site
*/
protected $url;

/**
* @var array request query vars (GET parameters)
*/
protected $queryVars;

/**
* @var string request body vars (POST parameters)
*/
protected $body;
public function setRequest(RequestInterface $request)
{
$this->_request = $request;

/**
* @var bool do not decode request
*/
protected $raw = false;
return $this;
}

/**
* XXX IN QUESTION
* Sends a request to retrieve data.
* In API this could be get, search or list request.
* @param array $options
* @throws ErrorResponseException
* @return mixed
*/
public function search($options = [])
public function search()
{
return $this->makeRequest('search', $options);
return $this->execute();
}

/**
* Sends a request to create/insert data.
* @param mixed $from entity to create
* @param mixed $data attributes of object to create
* @param mixed $options operation options
* @param mixed $table entity to create
* @param mixed $columns attributes of object to create
* @return mixed
*/
public function create($from, $data, array $options = [])
public function insert($table, $columns, array $options = [])
{
$this->query->from($from)->addParts($data);
$request = $this->db->getQueryBuilder()->insert($table, $columns, $options);

return $this->makeRequest('create', $options);
return $this->setRequest($request);
}

public function update($index, $data, $where, $options = [])
public function update($table, $columns, $condition = [], array $options = [])
{
$options['id'] = $id;

return $this->db->put($index . 'Update', array_merge($data, $options));
$request = $this->db->getQueryBuilder()->update($table, $columns, $condition, $options);

return $this->makeRequest('update', $options);
return $this->setRequest($request);
}

public function get($modelName, $primaryKey, $options = [])
public function delete($table, $condition, array $options = [])
{
return $this->db->post($modelName . 'GetInfo', ArrayHelper::merge(['id' => $primaryKey], $options));
}

public function mget($index, $type, $ids, $options = [])
{
$body = Json::encode(['ids' => array_values($ids)]);
$request = $this->db->getQueryBuilder()->delete($table, $condition, $options);

return $this->db->post([$index, $type, '_mget'], $options, $body);
}

public function exists($index, $type, $id)
{
return $this->db->head([$index, $type, $id]);
}

public function delete($from, $id, $options = [])
{
$this->query->from($from)->where(['id' => $id]);

return $this->makeRequest('delete', $options);
return $this->setRequest($request);
}

/**
* Performs str
* Executes the request.
* @param string $url URL
* @param mixed $body request parameters
* @return mixed
*/
public function perform($url, $body = [])
{
return $this->db->post($url, [], $body);
return $this->makeRequest($action, $options);
}

public function makeRequest($method, $action, array $options = [])
{
return $this->db->makeRequest(
$this->buildMethod($action, $options),
$this->buildUrl($action, $options),
$this->buildQuery($action, $options),
$this->buildBody($action, $options),
$this->buildRaw($action, $options)
);
}

public function buildUrl($action, array $options)
public function execute()
{
return $query->from . Inflector::id2camel($action);
var_dump($this->_request);
die();
return $this->db->send($this->_request);
}

public function getQueryVars($action, $options)
{
}

public function getBody($action, $options)
/**
* Creates and executes request with given data.
* @param string $action
* @param mixed $body request parameters
* @return mixed
*/
public function perform($action, $body = [])
{
}
$request = $this->db->getQueryBuilder()->perform($action, $body);
$this->setRequest($request);

public function getRaw($action, $options)
{
return $this->raw;
return $this->execute();
}
}
25 changes: 14 additions & 11 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

use Closure;
use GuzzleHttp\Client as Handler;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
Expand Down Expand Up @@ -239,16 +241,6 @@ public function delete($url, $query = [], $body = null, $raw = false)
return $this->makeRequest('DELETE', $url, $query, $body, $raw);
}

/**
* XXX DEPRECATED in favour of post().
* @param $url
* @param array $query
* @return mixed
*/
public function perform($url, $body = [])
{
return $this->makeRequest('DELETE', $url, [], $body);
}

/**
* Make request and check for error.
Expand Down Expand Up @@ -329,11 +321,22 @@ public function getHandler()
* Set handler manually.
* @param Handler $value
*/
public function setHandler($value)
public function setHandler(Handler $value)
{
static::$_handler = $value;
}

/**
* Sends given request.
* @param RequestInterface $request
* @param array $options
* @return ResponseInterface
*/
public function send(RequestInterface $request, array $options = [])
{
return $this->getHandler()->send($request, $options);
}

/**
* @return boolean
*/
Expand Down
Loading

0 comments on commit 95bd315

Please sign in to comment.