Skip to content

Commit

Permalink
Add CronManager library
Browse files Browse the repository at this point in the history
  • Loading branch information
temafey committed Feb 7, 2014
0 parents commit abe3529
Show file tree
Hide file tree
Showing 49 changed files with 6,817 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
vendor/*
.idea/*
64 changes: 64 additions & 0 deletions CronManager/Cron/FieldFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* @namespace
*/
namespace CronManager\Cron;

use InvalidArgumentException;

/**
* CRON field factory implementating a flyweight factory
*
* @author Michael Dowling <[email protected]>
* @link http://en.wikipedia.org/wiki/Cron
*/
class FieldFactory
{
/**
* @var array Cache of instantiated fields
*/
private $fields = array();

/**
* Get an instance of a field object for a cron expression position
*
* @param int $position CRON expression position value to retrieve
*
* @return FieldInterface
* @throws InvalidArgumentException if a position is not valide
*/
public function getField($position)
{
if (!isset($this->fields[$position])) {
switch ($position) {
case 0:
$this->fields[$position] = new SecondsField();
break;
case 1:
$this->fields[$position] = new \Cron\MinutesField();
break;
case 2:
$this->fields[$position] = new \Cron\HoursField();
break;
case 3:
$this->fields[$position] = new \Cron\DayOfMonthField();
break;
case 4:
$this->fields[$position] = new \Cron\MonthField();
break;
case 5:
$this->fields[$position] = new \Cron\DayOfWeekField();
break;
case 6:
$this->fields[$position] = new \Cron\YearField();
break;
default:
throw new InvalidArgumentException(
$position . ' is not a valid position'
);
}
}

return $this->fields[$position];
}
}
46 changes: 46 additions & 0 deletions CronManager/Cron/SecondsField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* @namespace
*/
namespace CronManager\Cron;

use DateTime,
DateInterval,
Cron\AbstractField;

/**
* Seconds field. Allows: * , / -
*
*/
class SecondsField extends AbstractField
{
/**
* {@inheritdoc}
*/
public function isSatisfiedBy(DateTime $date, $value)
{
return $this->isSatisfied($date->format('s'), $value);
}

/**
* {@inheritdoc}
*/
public function increment(DateTime $date, $invert = false)
{
if ($invert) {
$date->sub(new DateInterval('PT1S'));
} else {
$date->add(new DateInterval('PT1S'));
}

return $this;
}

/**
* {@inheritdoc}
*/
public function validate($value)
{
return (bool) preg_match('/[\*,\/\-0-9]+/', $value);
}
}
21 changes: 21 additions & 0 deletions CronManager/Download/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* @namespace
*/
namespace CronManager\Download\Adapter;

/**
* Interface AdapterInterface
* @package CronManager\Download\Adapter
*/
interface AdapterInterface
{
/**
* Download content by url to path
*
* @param string|array|\stdClass $uri
* @param string $path
* @return boolean
*/
public function download($uri, $path);
}
50 changes: 50 additions & 0 deletions CronManager/Download/Adapter/Curl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* @namespace
*/
namespace CronManager\Download\Adapter;

use CronManager\Download\DownloadInterface;

/**
* Class Curl
* @package CronManager\Download\Adapter
*/
class Curl implements DownloadInterface
{
/**
* Cmd
* @var string
*/
private $_cmd = 'curl -C ';

/**
* Bin path
* @var string
*/
private $_binPath = '';

/**
* Download
*
* @param string $url
* @param string $path
* @return string
*/
public function download($url, $path)
{
return shell_exec($this->_binPath.$this->_cmd." -o ".$path." '".$url."'");
}

/**
* Set bin path
*
* @param string $path
* @return \CronManager\Download\Adapter\Wget
*/
public function setBinPath($path)
{
$this->_binPath = rtrim($path, '/').'/';
return $this;
}
}
85 changes: 85 additions & 0 deletions CronManager/Download/Adapter/Wget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* @namespace
*/
namespace CronManager\Download\Adapter;

use CronManager\Download\DownloadInterface;

/**
* Class Wget
* @package CronManager\Download\Adapter
*/
class Wget implements DownloadInterface
{

//wget -c -t 0 --timeout=15
//wget --ftp-user=USERNAME --ftp-password=PASSWORD DOWNLOAD-URL
/**
* Cmd
* @var string
*/
private $_cmd = 'wget -c'; //-b in background';

/**
* Bin path
* @var string
*/
private $_binPath = '';

/**
* Download
*
* @param string|array|\stdClass $uri
* @param string $path
* @return string
*/
public function download($uri, $path)
{
$cmd = $this->_binPath.$this->_cmd;
$log = "";
if (is_string($uri)) {
$cmd .= ' "'.$uri.'"';
} elseif (is_array($uri)) {
if (isset($uri['url'])) {
$cmd .= ' "'.$uri['url'].'"';
}
if (isset($uri['user'])) {
$cmd .= ' --user='.$uri['user'];
}
if (isset($uri['pass'])) {
$cmd .= ' --password='.$uri['pass'];
}
if (isset($uri['log'])) {
$log = "-o ".$uri['log'];
}
} elseif (is_object($uri)) {
if (isset($uri->url)) {
$cmd .= ' "'.$uri->url.'"';
}
if (isset($uri->user)) {
$cmd .= ' --user='.$uri->user;
}
if (isset($uri->pass)) {
$cmd .= ' --password='.$uri->pass;
}
if (isset($uri->log)) {
$log = "-o ".$uri->log;
}
}

return shell_exec($cmd.' -P '.$path." ".$log);
}

/**
* Set bin path
*
* @param string $path
* @return \CronManager\Download\Adapter\Wget
*/
public function setBinPath($path)
{
$this->_binPath = rtrim($path, '/').'/';
return $this;
}
}
91 changes: 91 additions & 0 deletions CronManager/Download/Download.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* @namespace
*/
namespace CronManager\Download;

use CronManager\Download\Adapter\AdapterInterface,
CronManager\Download\Adapter\Curl,
CronManager\Download\Adapter\Wget;

/**
* Class Download
* @package CronManager\Download
*/
class Download implements DownloadInterface
{
CONST ADAPTER_WGET = 'wget';
CONST ADAPTER_CURL = 'curl';

/**
* Download adapter
* @var \CronManager\Download\Adapter\AdapterInterface
*/
protected $_adapter;

/**
* Initiate
*
* @param array $options
*/
public function __construct(array $options)
{
$this->setOptions($options);
}

/**
* Set options
*
* @param array $options
* @return \CronManager\Download\Download
*/
public function setOptions(array $options)
{
if (array_key_exists('adapter', $options)) {
$this->setAdapter($options['adapter']);
}
}

/**
* Set download adapter
*
* @param array|string|AdapterInterface $adapter
* @return \CronManager\Download\Download
*/
public function setAdapter($adapter)
{
if ($adapter instanceof AdapterInterface) {
$this->_adapter = $adapter;
return $this;
}

if (is_string($adapter)) {
switch ($adapter) {
case Download::ADAPTER_CURL:
$this->_adapter = new Curl();
break;
case Download::ADAPTER_WGET:
$this->_adapter = new Wget();
break;
default:
throw new \Exception("Download adapter not set!");
break;
}
}

return $this;
}

/**
* Download file
*
* @param string $uri
* @param string $path
* @return boolean
*/
public function download($uri, $path)
{
return $this->_adapter->download($uri, $path);
}
}

21 changes: 21 additions & 0 deletions CronManager/Download/DownloadInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* @namespace
*/
namespace CronManager\Download;

/**
* Interface DownloadInterface
* @package CronManager\Download
*/
interface DownloadInterface
{
/**
* Download contetn by link to path
*
* @param string $resource
* @paran string $path
* @return bollean
*/
public function download($uri, $path);
}
Loading

0 comments on commit abe3529

Please sign in to comment.