Skip to content

Commit

Permalink
Uploading base project
Browse files Browse the repository at this point in the history
  • Loading branch information
litmanowicziv committed Jul 23, 2016
1 parent 1b200c1 commit 00046fc
Show file tree
Hide file tree
Showing 13 changed files with 1,134 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
composer.phar
/vendor/

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
composer.lock
/.settings/
.buildpath
.project
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "litmanovitziv/struct-reader",
"type": "library",
"description": "Simple library to read multiple data sources",
"keywords": ["reader", "data sources"],
"homepage": "https://github.com/oscarotero/env",
"authors": [
{
"name": "Ziv Litmanovitz",
"email": "[email protected]",
"homepage": "https://www.linkedin.com/in/zivlitmanovitz",
"role": "Software Developer and Data Analyst"
}
],
"support": {
"email": "[email protected]",
"issues": "https://github.com/oscarotero/env/issues"
},
"require": {
"php": ">=5.6.0",
"ospinto/dbug": "dev-master"
},
"autoload": {
}
}
59 changes: 59 additions & 0 deletions src/CSVFeed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

set_include_path("./../". PATH_SEPARATOR . ini_get("include_path"));

require_once 'vendor/autoload.php';
require_once 'src/Feed.php';

// TODO : Using fgetCSV() : http://php.net/manual/en/function.fgetcsv.php
class CSVFeed extends Feed {
private $_header;
private $_delimiter;

function __construct($file, $delimiter) {
parent::__construct();

if (($this->_file_handler = fopen($file, "r")) == false)
throw new Exception("Error: Invalid file");

if (isset($delimiter))
$this->_delimiter = $delimiter;
else throw new Exception("Error: Not defined delimiter");

if (($this->_header = fgetcsv($this->_file_handler, 0, $this->_delimiter)) == false)
throw new Exception("Reading of Header was failed");

if (isDebugEnv() & ENV_DEBUG_PRINTED)
new dBug($this->_file_handler);
}

function __destruct() {
fclose($this->_file_handler);
}

function valid() {
if (parent::valid())
return !feof($this->_file_handler);
}

function next() {
if (($this->_entity = fgetcsv($this->_file_handler, 0, $this->_delimiter)) == false)
throw new Exception("Error: product failed on read");

// Handling blamk row
if (empty(implode($this->_entity)))
throw new Exception("Error: Record is empty");

$this->_entity = array_combine($this->_header, $this->_entity);
parent::next();
}

function rewind() {
parent::rewind();
rewind($this->_file_handler);

if (($this->_header = fgetcsv($this->_file_handler, 0, $this->_delimiter)) == false)
throw new Exception("Reading of Header was failed");
}

}
150 changes: 150 additions & 0 deletions src/Env.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

//define('NEW_LINE', '<br />');
define('NEW_LINE', "<br />\r\n");

define("ENV_DEBUG", 1 << 8);
define("ENV_DEBUG_PRINTED", 1);
define("ENV_DEBUG_LIMITED", 1 << 1);
define("ENV_DEBUG_TESTED", 1 << 2);
define("ENV_DEBUG_IMPORT", 1 << 3);
define("ENV_DEBUG_EXPORT", 1 << 4);

define("ENV_LOCAL", 1 << 9);
define("ENV_OS_MAC", 1);
define("ENV_OS_LINUX", 1 << 1);
define("ENV_OS_WIN", 1 << 2);

define("ENV_AMAZON", 1 << 10);
define("ENV_GOOGLE", 1 << 11);
define("ENV_PROD_API", 1);
define("ENV_PROD_DEV", 1 << 1);
define("ENV_PROD_CRAWL", 1 << 2);



function getEnvRunning() {

$hostname = gethostname();
switch ($hostname) {
case 'api.shopnfly.com' :
return ENV_GOOGLE | ENV_PROD_API;
case 'api3.shopnfly.com' :
return ENV_GOOGLE | ENV_PROD_DEV;
case 'crawl.shopnfly.com' :
return ENV_GOOGLE | ENV_CRAWL;
case 'ip-10-97-27-41' :
return ENV_AMAZON | ENV_PROD_API;
case 'dev.shopnfly.com' :
return ENV_AMAZON | ENV_PROD_DEV;
case 'Eylon-Steiners-MacBook-Pro.local' :
return ENV_LOCAL | ENV_OS_MAC;
case 'EylonsMacBookPro.local' :
return ENV_LOCAL | ENV_OS_MAC;
case 'zivlit-HP-ProBook-4530s' :
return ENV_LOCAL | ENV_OS_LINUX;
case 'zivshopnfly' :
return ENV_LOCAL | ENV_OS_LINUX;
default :
return 0;
}
}

function getEnvUrl() {
$host = getEnvRunning();
if ($host & ENV_LOCAL) {
if ($host & ENV_OS_MAC)
return 'localhost/df/public';
else if ($host & ENV_OS_LINUX)
return 'localhost/shopnfly_server/public';
} else {
if ($host & ENV_PROD_API)
return 'api.shopnfly.com';
else if ($host & ENV_PROD_DEV)
return 'api3.shopnfly.com';
}
}

function getWebUrl() {
$host = getEnvRunning();
if ($host & ENV_PROD_API)
return 'https://www.shopnfly.com';
else if ($host & ENV_PROD_DEV)
return 'https://web3.shopnfly.com';
else return 'https://https://www.shopnfly.com';
}

function getEnvEmail() {
$env = getEnvRunning();
if ($env & ENV_LOCAL) {
if (stripos(gethostname(), 'ziv'))
return "[email protected]";
if (stripos(gethostname(), 'eylon'))
return "[email protected]";
} else return '[email protected]';
}

function getEnvName() {
$env = getEnvRunning();
if ($env & ENV_LOCAL)
return 'localhost';
else if ($env & ENV_PROD_DEV)
return 'dev';
else if ($env & ENV_PROD_API)
return 'api';
}

function isDebugEnv() {
$status = false;

if (isset($_REQUEST['debug']) && !empty($_REQUEST['debug'])) {
$optionsArr = explode(',', $_REQUEST['debug']);

if (!empty($optionsArr)) {
$status = ENV_DEBUG;
foreach ($optionsArr as $option)
switch ($option) {
case 'print':
$status = $status | ENV_DEBUG_PRINTED;
break;
case 'limit':
$status = $status | ENV_DEBUG_LIMITED;
break;
case 'test':
$status = $status | ENV_DEBUG_TESTED;
break;
case 'import':
$status = $status | ENV_DEBUG_IMPORT;
break;
case 'export':
$status = $status | ENV_DEBUG_EXPORT;
break;
default:
$status = $status | true;
break;
}
}
}

return $status;
}

function getMaxIterarions() {
if (isDebugEnv() & ENV_DEBUG_LIMITED)
return intval($_REQUEST['limit']);
return null;
}

function showInDebugEnv($var) {

if (isset($_REQUEST['debug']) && ($_REQUEST['debug'] == 'true')) {
new dBug($var);
}

if (php_sapi_name() === 'cli') {
print_r($var);
echo PHP_EOL;
}
}

?>
57 changes: 57 additions & 0 deletions src/Feed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

// http://localhost/crawl/read/<type>Feed.php?debug=true,print,test,limit&limit=1
set_include_path("./../". PATH_SEPARATOR . ini_get("include_path"));

require_once 'vendor/autoload.php';
require_once 'src/Env.php';

abstract class Feed implements Iterator {
protected $_file_handler;
protected $_entity;
protected $_record_index;

/**
* TODO : adding debug configuration
* @param array $debug_config
*/
function __construct($debug_config = null) {
$this->_record_index = 0;
}

function getEntity() {
return $this->_entity;
}

function rewind() {
$this->_record_index = 0;
}

function valid() {
if (isDebugEnv() & ENV_DEBUG_LIMITED) {
if ($this->_record_index > getMaxIterarions()) {
$this->_record_index = 0;
echo "Warning : Stopped because of limit is over<br />";
return false;
}
}

return true;
}

function next() {
$this->_record_index++;
}

function key() {
return $this->_record_index;
}

function current() {
if (isDebugEnv() & ENV_DEBUG_PRINTED) {
new dBug("record no. $this->_record_index");
new dBug($this->_entity);
}
}

}
39 changes: 39 additions & 0 deletions src/JSONFeed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

set_include_path("./../". PATH_SEPARATOR . ini_get("include_path"));

require_once 'vendor/autoload.php';
require_once 'src/Feed.php';

class JSONFeed extends Feed {

function __construct($file) {
parent::__construct();

if (($this->_file_handler = fopen($file, "r")) == false)
throw new Exception("Error: Invalid file");
}

function __destruct() {
fclose($this->_file_handler);
}

function valid() {
if (parent::valid())
return !feof($this->_file_handler);
}

function next() {
if (($this->_entity = fgets($this->_file_handler)) == false)
throw new Exception("Error: product failed on read");
if (($this->_entity = json_decode($this->_entity, true)) === null)
throw new Exception("Error: unrecognized type");
parent::next();
}

function rewind() {
parent::rewind();
rewind($this->_file_handler);
}

}
Loading

0 comments on commit 00046fc

Please sign in to comment.