Skip to content

Commit

Permalink
cradle pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
aprilsacil committed May 27, 2018
0 parents commit 3894b3b
Show file tree
Hide file tree
Showing 14 changed files with 1,312 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .cradle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php //-->
/**
* This file is part of a Custom Package.
*/
require_once __DIR__ . '/src/events.php';
require_once __DIR__ . '/src/controller.php';

//bootstrap
$this->preprocess(include __DIR__ . '/src/helpers.php');
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
composer.phar
/vendor/
/node_modules/
/bin/
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 April Sacil

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Cradle Pipeline Package

Pipeline manager.

## Install

```
composer install cradlephp/cradle-pipeline
```

## Pipeline

Cradle Pipeline handles everything about the pipeline. It is based on CradlePHP/cradle-system.

### Pipeline Routes

The following routes are available in the admin.

- `GET /admin/system/model/:schema/pipeline` - Pipeline search page
- `POST /admin/system/model/:schema/pipeline` - Pipeline processor

### Pipeline Events

- `pipeline-update`
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "cradlephp/cradle-pipeline",
"type": "cradle-package",
"description": "Profile Schema for CradlePHP",
"minimum-stability": "dev",
"keywords": [
"cradle",
"cradlephp"
],
"license": "MIT",
"authors": [
{
"name": "April Sacil",
"email": "asacil@sterlingtech.ph"
}
],
"require-dev": {
"phpunit/phpunit": "7.0.2",
"squizlabs/php_codesniffer": "3.2.3",
"satooshi/php-coveralls": "2.0.0"
},
"require": {
"cradlephp/cradle-system": "1.*"
}
}
58 changes: 58 additions & 0 deletions src/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php //-->
/**
* This file is part of a package designed for the CradlePHP Project.
*
* Copyright and license information can be found at LICENSE.txt
* distributed with this package.
*/
namespace Cradle\Package\Pipeline;

use Cradle\Package\Pipeline\Service\SqlService;
use Cradle\Package\System\Model\Service\RedisService;
use Cradle\Package\System\Model\Service\ElasticService;

use Cradle\Module\Utility\Service\NoopService;
use Cradle\Module\Utility\ServiceInterface;

/**
* Service layer
*
* @vendor Cradle
* @package System
* @author Christan Blanquera <cblanquera@openovate.com>
* @standard PSR-2
*/
class Service
{
/**
* Returns a service. To prevent having to define a method per
* service, instead we roll everything into one function
*
* @param *string $name
* @param string $key
*
* @return object
*/
public static function get($name, $key = 'main')
{
if (in_array($name, ['sql', 'redis', 'elastic'])) {
$resource = cradle('global')->service($name . '-' . $key);

if ($resource) {
if ($name === 'sql') {
return new SqlService($resource);
}

if ($name === 'redis') {
return new RedisService($resource);
}

if ($name === 'elastic') {
return new ElasticService($resource);
}
}
}

return new NoopService();
}
}
96 changes: 96 additions & 0 deletions src/Service/SqlService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php //-->
/**
* This file is part of a package designed for the CradlePHP Project.
*
* Copyright and license information can be found at LICENSE.txt
* distributed with this package.
*/

namespace Cradle\Package\Pipeline\Service;

use PDO as Resource;
use Cradle\Storm\SqlFactory;

use Cradle\Module\Utility\Service\SqlServiceInterface;
use Cradle\Module\Utility\Service\AbstractSqlService;

use Cradle\Package\System\Model\Service\SqlService as ModelService;

use Cradle\Package\System\Schema;
use Cradle\Package\System\Exception as SystemException;

/**
* Model SQL Service
*
* @vendor Cradle
* @package System
* @author Christan Blanquera <cblanquera@openovate.com>
* @standard PSR-2
*/
class SqlService extends ModelService
{
/**
* Update to database
*
* @param array $data
*
* @return array
*/
public function update(array $data)
{
if (is_null($this->schema)) {
throw SystemException::forNoSchema();
}

$table = $this->schema->getName();
$updated = $this->schema->getUpdatedFieldName();

if ($updated) {
$data[$updated] = date('Y-m-d H:i:s');
}

// we will be using moved, since
// 'order' is used for sorting
// if there is moved, the the user
// attempts to update ordering
if (isset($data['moved'])) {
$query = $this->resource
->getUpdateQuery($table)
->where($data['filters']);

if (!isset($data['fields'])) {
$data['fields'] = [];
}

// update the update field if any
if ($updated) {
$data['fields'][$updated] = "'" . $data[$updated] . "'";
}

// add fields to be updated
foreach ($data['fields'] as $field => $value) {
$query->set($field, $value);
}

if (isset($data['previous_column']) && $data['previous_column']) {
cradle()->inspect($query->getQuery());
}

// we need to assign as we might end from here
$results = $this->resource->query($query);
}

// if this is for the previous column update only
// we have nothing to update against a specific
// column, so we have to go back from here
if (isset($data['previous_column']) && $data['previous_column']) {
return [];
}

return $this
->resource
->model($data)
->save($table)
->get();
}
}
Loading

0 comments on commit 3894b3b

Please sign in to comment.