Skip to content

Commit

Permalink
add code in ALPHA version
Browse files Browse the repository at this point in the history
  • Loading branch information
bieli committed Jan 15, 2010
1 parent 73b186d commit c1fdffe
Show file tree
Hide file tree
Showing 8 changed files with 432 additions and 0 deletions.
64 changes: 64 additions & 0 deletions SimpleCronTaskBoot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

define('LIBRARY_PATH', dirname(__FILE__) . '/lib');
define('DATA_PATH', dirname(__FILE__) . '/data');
define('LOGS_PATH', dirname(__FILE__) . '/log');
define('BATCH_PATH', dirname(__FILE__) . '/batch');


function simple_class_autoloader($i_ClassName)
{
$_classFilePath = LIBRARY_PATH . '/' . $i_ClassName . '.class.php';

if ( true === file_exists($_classFilePath) )
{
require_once($_classFilePath);
return true;
}
else
{
throw new Exception(sprintf('Could not load class "%s"'
. ' from file "%s"', $i_ClassName, $_classFilePath));
}
}

if ( false === function_exists('spl_autoload_register') )
{
die('Unable to use function "spl_autoload_register" !!!');
}

spl_autoload_register('simple_class_autoloader');

$_registredClases = array(
'SimpleFileLogger',
'PropertiesManager',
'SimpleCronTask'
);

foreach ( $_registredClases as $_class_Key => $_classNameValue )
{
$_tmpObj = new $_classNameValue();
unset($_tmpObj);
}

//var_dump(LIBRARY_PATH, $_registredClases);

/*
$_libsDir = opendir(LIBRARY_PATH);
if ( true === is_resource($_libsDir) )
{
while ( $_className = readdir($_libsDir) )
{
$_registredClases[] = $_className;
echo $_className . "\n";
$_tmpObj = new $_className();
unset($_tmpObj);
}
closedir($_libsDir);
var_dump($_registredClases);
}
*/

4 changes: 4 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- refactoring for logger class
- optimization source
- use implementation for tasks
- write free domains in database
93 changes: 93 additions & 0 deletions batch/GetFreeDomainsFromNaskSimpleCronTask.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

class GetFreeDomainsFromNaskSimpleCronTask
//TODO: extends SimpleCronTask
//TODO: implements SimpleCronTaskInterface
{
const DATETIME_FORMAT = 'Ymd_His';

public function __construct()
{
$this->properties = array(
'nask_domains_contents_address'
=> 'http://www.dns.pl/deleted_domains.txt',
'output_file_format'
=> sprintf('deleted_domains.%s.xml',
self::DATETIME_FORMAT)

);
}

public function GetParams($i_Parametrs = array())
{

}

public function Run($i_Parametrs = array())
{
$_freeDomains = @file_get_contents(
$this->properties['nask_domains_contents_address']);

if ( true === is_string($_freeDomains)
&& 0 < strlen($_freeDomains) )
{
$_domains = explode("\n", $_freeDomains);

$_domainsAll = array();

$_updateTime = null;
foreach ( $_domains as $_domainValue )
{
$_domainValue = trim($_domainValue);
if ( 0 < strlen($_domainValue) )
{
$_domainsAll[ $_domainValue ] = $_domainValue;
}

if ( null === $_updateTime )
{
$_updateTime = $_domainsAll[ $_domainValue ];
unset($_domainsAll[ $_domainValue ]);
}
}

$this->taskLogger->Log('Got ' . count($_domainsAll)
. ' domains with update time "'
. trim($_updateTime) . '"');

$this->SaveXMLOutput($_updateTime, $_domainsAll);
}
else
{
$this->taskLogger->Log('Problem with getting domains from address: '
. $this->properties['nask_domains_contents_address'], 'error');
}

}

public function SetLogger($i_TaskLogger)
{
$this->taskLogger = $i_TaskLogger;
}

private function SaveXMLOutput($i_UpdateTime, $i_DomainsAll)
{
$_md5sum = md5(base64_encode(serialize($i_DomainsAll)));

$_outputDataPath = sprintf('%s/%s.%s.%s.xml', DATA_PATH, basename(__FILE__), date('Ymd'), $_md5sum);

$_xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><free_domains></free_domains>');

$_xml->addAttribute('update_time', $i_UpdateTime);
$_xml->addAttribute('download_unixtime', time());

$_domains = $_xml->addChild('domains');

foreach ( $i_DomainsAll as $_domainName )
{
$_domains->AddChild('domain_name', htmlentities($_domainName));
}

return $_xml->asXML($_outputDataPath);
}
}
42 changes: 42 additions & 0 deletions lib/PropertiesManager.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

class PropertiesManager extends SimpleFileLogger
{
const ERR_MSG_PATTERN = '%s: Not exists property "%s" in object "%s"';

protected $className = '';
protected $properties = array();

public function __construct($i_ClassName = __CLASS__)
{
$this->className = $i_ClassName;
$this->properties = get_class_vars($this->className);
}

public function __get($i_PropertyName)
{
if ( array_key_exists($i_PropertyName, $this->properties) )
{
return $this->$i_PropertyName;
}
else
{
throw new Exception(sprintf(self::ERR_MSG_PATTERN,
'__get()', $i_PropertyName, $this->className));
}
}

public function __set($i_PropertyName, $i_Value)
{
if ( array_key_exists($i_PropertyName, $this->properties) )
{
$this->$i_PropertyName = $i_Value;
}
else
{
throw new Exception(sprintf(self::ERR_MSG_PATTERN,
'__set()', $i_PropertyName, $this->className));
}
}
}

53 changes: 53 additions & 0 deletions lib/SimpleCronTask.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

class SimpleCronTask extends PropertiesManager
{
private $taskLogger = null;

public function __construct($i_TaskName = null)
{
$this->className = $i_TaskName;

if ( null !== $i_TaskName )
{
//TODO: remember about use properties
// date('Y-m-d H:i:s');
// $this->output_file_format
$_logPath = LOGS_PATH . '/' . $i_TaskName . '.class.php.'
. date('Ymd') . '.log';
//var_dump($_logPath);
$this->taskLogger = new SimpleFileLogger($_logPath, true);

$this->taskLogger->Log('START');

$_taskObj = $this->GetSimpleTaskInstance($i_TaskName);

//TODO: set parametrs
$_taskObj->GetParams(array());

$this->taskLogger->Log('RUN');

$_taskObj->Run(array());

//TODO: load task from batch
// $i_TaskName
//TODO: set additional properties
//TODO: run task procsess

$this->taskLogger->Log('STOP');
}
}

private function GetSimpleTaskInstance($i_TaskName)
{
$o_Instance = false;

require_once(BATCH_PATH . '/' . $i_TaskName . '.class.php');

$o_Instance = new $i_TaskName();
$o_Instance->SetLogger($this->taskLogger);

return $o_Instance;
}
}

7 changes: 7 additions & 0 deletions lib/SimpleCronTask.interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

interface SimpleCronTaskInterface
{
public function GetParams($i_Parametrs = array());
public function Run($i_Parametrs = array());
}
Loading

0 comments on commit c1fdffe

Please sign in to comment.