add: Onedrive Bussiness SDK

This commit is contained in:
HFO4 2018-09-08 13:25:21 +08:00
parent 044ece0b80
commit e29c94d8d0
9 changed files with 1692 additions and 1 deletions

View file

@ -33,7 +33,6 @@
"aliyuncs/oss-sdk-php": "~2.0",
"sabre/dav":"~3.2.0",
"upyun/sdk": "^3.3",
"krizalys/onedrive-php-sdk": "^1.2"
},
"autoload": {
"psr-0": {

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,298 @@
<?php
namespace Krizalys\Onedrive;
/**
* @class DriveItem
*
* A DriveItem instance is an entity that may be stored in a OneDrive account.
* There are two types of drive items: file or a folder, each of which being a
* subclass of the DriveItem class.
*
* Note that DriveItem instances are only "proxy" to actual OneDrive drive items
* (eg. destroying a DriveItem instance will not delete the actual OneDrive
* drive item it is referencing to).
*/
abstract class DriveItem
{
/**
* @var Client The owning Client instance.
*/
protected $_client;
/**
* @var string The unique ID assigned by OneDrive to this drive item.
*/
protected $_id;
/**
* @var string The unique ID assigned by OneDrive to the parent folder of
* this drive item.
*/
private $_parentId;
/**
* @var string The name of this drive item.
*/
private $_name;
/**
* @var string The description of this drive item.
*/
private $_description;
/**
* @var int The size of this drive item, in bytes.
*/
private $_size;
/**
* @var string The source link of this drive item.
*/
private $_source;
/**
* @var int The creation time, in seconds since UNIX epoch.
*/
private $_createdTime;
/**
* @var int The last modification time, in seconds since UNIX epoch.
*/
private $_updatedTime;
/**
* Constructor.
*
* @param Client $client The Client instance owning this DriveItem
* instance.
* @param null|string $id The unique ID of the OneDrive drive item
* referenced by this DriveItem instance.
* @param array|object $options An array/object with one or more of the
* following keys/properties:
* - 'parent_id' (string) The unique ID of
* the parent OneDrive folder of this drive
* item.
* - 'name' (string) The name of this drive
* item.
* - 'description' (string) The description of
* this drive item. May be empty.
* - 'size' (int) The size of this drive item,
* in bytes.
* - 'source' (string) The source link of this
* drive item.
* - 'created_time' (string) The creation time,
* as a RFC date/time.
* - 'updated_time' (string) The last
* modification time, as a RFC date/time.
*/
public function __construct(Client $client, $id, $options = [])
{
$options = (object) $options;
$this->_client = $client;
$this->_id = null !== $id ? (string) $id : null;
$this->_parentId = property_exists($options, 'parent_id') ?
(string) $options->parent_id : null;
$this->_name = property_exists($options, 'name') ?
(string) $options->name : null;
$this->_description = property_exists($options, 'description') ?
(string) $options->description : null;
$this->_size = property_exists($options, 'size') ?
(int) $options->size : null;
$this->_source = property_exists($options, 'source') ?
(string) $options->source : null;
$this->_createdTime = property_exists($options, 'created_time') ?
strtotime($options->created_time) : null;
$this->_updatedTime = property_exists($options, 'updated_time') ?
strtotime($options->updated_time) : null;
}
/**
* Determines whether the OneDrive drive item referenced by this DriveItem
* instance is a folder.
*
* @return bool true if the OneDrive drive item referenced by this DriveItem
* instance is a folder, false otherwise.
*/
public function isFolder()
{
return false;
}
/**
* Fetches the properties of the OneDrive drive item referenced by this
* DriveItem instance. Some properties are cached for faster subsequent
* access.
*
* @return array The properties of the OneDrive drive item referenced by
* this DriveItem instance.
*/
public function fetchProperties()
{
$result = $this->_client->fetchProperties($this->_id);
$this->_parentId = '' != $result->parent_id ?
(string) $result->parent_id : null;
$this->_name = $result->name;
$this->_description = '' != $result->description ?
(string) $result->description : null;
$this->_size = (int) $result->size;
/** @todo Handle volatile existence (eg. present only for files). */
$this->_source = (string) $result->source;
$this->_createdTime = strtotime($result->created_time);
$this->_updatedTime = strtotime($result->updated_time);
return $result;
}
/**
* Gets the unique ID of the OneDrive drive item referenced by this
* DriveItem instance.
*
* @return string The unique ID of the OneDrive drive item referenced by
* this DriveItem instance.
*/
public function getId()
{
return $this->_id;
}
/**
* Gets the unique ID of the parent folder of the OneDrive drive item
* referenced by this DriveItem instance.
*
* @return string The unique ID of the OneDrive folder containing the drive
* item referenced by this DriveItem instance.
*/
public function getParentId()
{
if (null === $this->_parentId) {
$this->fetchProperties();
}
return $this->_parentId;
}
/**
* Gets the name of the OneDrive drive item referenced by this DriveItem
* instance.
*
* @return string The name of the OneDrive drive item referenced by this
* DriveItem instance.
*/
public function getName()
{
if (null === $this->_name) {
$this->fetchProperties();
}
return $this->_name;
}
/**
* Gets the description of the OneDrive drive item referenced by this
* DriveItem instance.
*
* @return string The description of the OneDrive drive item referenced by
* this DriveItem instance.
*/
public function getDescription()
{
if (null === $this->_description) {
$this->fetchProperties();
}
return $this->_description;
}
/**
* Gets the size of the OneDrive drive item referenced by this DriveItem
* instance.
*
* @return int The size of the OneDrive drive item referenced by this
* DriveItem instance.
*/
public function getSize()
{
if (null === $this->_size) {
$this->fetchProperties();
}
return $this->_size;
}
/**
* Gets the source link of the OneDrive drive item referenced by this
* DriveItem instance.
*
* @return string The source link of the OneDrive drive item referenced by
* this DriveItem instance.
*/
public function getSource()
{
if (null === $this->_source) {
$this->fetchProperties();
}
return $this->_source;
}
/**
* Gets the creation time of the OneDrive drive item referenced by this
* DriveItem instance.
*
* @return int The creation time of the drive item referenced by this
* DriveItem instance, in seconds since UNIX epoch.
*/
public function getCreatedTime()
{
if (null === $this->_createdTime) {
$this->fetchProperties();
}
return $this->_createdTime;
}
/**
* Gets the last modification time of the OneDrive drive item referenced by
* this DriveItem instance.
*
* @return int The last modification time of the drive item referenced by
* this DriveItem instance, in seconds since UNIX epoch.
*/
public function getUpdatedTime()
{
if (null === $this->_updatedTime) {
$this->fetchProperties();
}
return $this->_updatedTime;
}
/**
* Moves the OneDrive drive item referenced by this DriveItem instance into
* another OneDrive folder.
*
* @param null|string $destinationId The unique ID of the OneDrive folder
* into which to move the OneDrive drive
* item referenced by this DriveItem
* instance, or null to move it to the
* OneDrive root folder. Default: null.
*/
public function move($destinationId = null)
{
$this->_client->moveObject($this->_id, $destinationId);
}
}

View file

@ -0,0 +1,71 @@
<?php
namespace Krizalys\Onedrive;
/**
* @class File
*
* A File instance is a DriveItem instance referencing a OneDrive file. It may
* have content but may not contain other OneDrive drive items.
*/
class File extends DriveItem
{
/**
* Constructor.
*
* @param Client $client The Client instance owning this DriveItem
* instance.
* @param null|string $id The unique ID of the OneDrive drive item
* referenced by this DriveItem instance.
* @param array|object $options An array/object with one or more of the
* following keys/properties:
* - 'parent_id' (string) The unique ID of the
* parent OneDrive folder of this drive item.
* - 'name' (string) The name of this drive
* item.
* - 'description' (string) The description of
* this drive item. May be empty.
* - 'size' (int) The size of this drive item,
* in bytes.
* - 'created_time' (string) The creation time,
* as a RFC date/time.
* - 'updated_time' (string) The last
* modification time, as a RFC date/time.
*/
public function __construct(Client $client, $id, $options = [])
{
parent::__construct($client, $id, $options);
}
/**
* Fetches the content of the OneDrive file referenced by this File
* instance.
*
* @param array $options Extra cURL options to apply.
*
* @return string The content of the OneDrive file referenced by this File
* instance.
*
* @todo Should somewhat return the content-type as well; this information
* is not disclosed by OneDrive.
*/
public function fetchContent($options = [])
{
return $this->_client->apiGet($this->_id . '/content', $options);
}
/**
* Copies the OneDrive file referenced by this File instance into another
* OneDrive folder.
*
* @param null|string $destinationId The unique ID of the OneDrive folder
* into which to copy the OneDrive file
* referenced by this File instance, or
* null to copy it in the OneDrive root
* folder. Default: null.
*/
public function copy($destinationId = null)
{
$this->_client->copyFile($this->_id, $destinationId);
}
}

View file

@ -0,0 +1,143 @@
<?php
namespace Krizalys\Onedrive;
/**
* @class Folder
*
* A Folder instance is a DriveItem instance referencing to a OneDrive folder.
* It may contain other OneDrive drive items but may not have content.
*/
class Folder extends DriveItem
{
/**
* Determines whether the OneDrive drive item referenced by this DriveItem
* instance is a folder.
*
* @return bool true if the OneDrive drive item referenced by this DriveItem
* instance is a folder, false otherwise.
*/
public function isFolder()
{
return true;
}
/**
* Constructor.
*
* @param Client $client The Client instance owning this DriveItem
* instance.
* @param null|string $id The unique ID of the OneDrive drive item
* referenced by this DriveItem instance, or
* null to reference the OneDrive root folder.
* Default: null.
* @param array|object $options Options to pass to the DriveItem
* constructor.
*/
public function __construct(Client $client, $id = null, $options = [])
{
parent::__construct($client, $id, $options);
}
/**
* Gets the drive items in the OneDrive folder referenced by this Folder
* instance.
*
* @return array The drive items in the OneDrive folder referenced by this
* Folder instance, as DriveItem instances.
*
* @deprecated Use Folder::fetchChildObjects() instead.
*/
public function fetchObjects()
{
/** @todo Log deprecation notice. */
return $this->fetchChildObjects();
}
/**
* Gets the child drive items in the OneDrive folder referenced by this
* Folder instance.
*
* @return array The drive items in the OneDrive folder referenced by this
* Folder instance, as DriveItem instances.
*/
public function fetchChildObjects()
{
return $this->_client->fetchObjects($this->_id);
}
/**
* Gets the descendant drive items under the OneDrive folder referenced by
* this Folder instance.
*
* @return array The files in the OneDrive folder referenced by this Folder
* instance, as DriveItem instances.
*/
public function fetchDescendantObjects()
{
$driveItems = [];
foreach ($this->fetchChildObjects() as $driveItem) {
if ($driveItem->isFolder()) {
$driveItems = array_merge(
$driveItem->fetchDescendantObjects(),
$driveItems
);
} else {
array_push($driveItems, $driveItem);
}
}
return $driveItems;
}
/**
* Creates a folder in the OneDrive folder referenced by this Folder
* instance.
*
* @param string $name The name of the OneDrive folder to be
* created.
* @param null|string $description The description of the OneDrive folder
* to be created, or null to create it
* without a description. Default: null.
*
* @return Folder The folder created, as a Folder instance.
*/
public function createFolder($name, $description = null)
{
return $this->_client->createFolder($name, $this->_id, $description);
}
/**
* Creates a file in the OneDrive folder referenced by this Folder instance.
*
* @param string $name The name of the OneDrive file to be
* created.
* @param string|resource $content The content of the OneDrive file to be
* created, as a string or handle to an
* already opened file. In the latter case,
* the responsibility to close the handle is
* is left to the calling function. Default:
* ''.
* @param array $options The options.
*
* @return File The file created, as a File instance.
*
* @throws \Exception Thrown on I/O errors.
*/
public function createFile($name, $content = '', array $options = [])
{
$client = $this->_client;
$options = array_merge([
'name_conflict_behavior' => $client->getNameConflictBehavior(),
'stream_back_end' => $client->getStreamBackEnd(),
], $options);
return $this->_client->createFile(
$name, $this->_id,
$content,
$options
);
}
}

View file

@ -0,0 +1,17 @@
<?php
namespace Krizalys\Onedrive;
class NameConflictBehavior
{
// Fail behavior: fail the operation if the drive item exists.
const FAIL = 1;
// Rename behavior: rename the drive item if it already exists. The drive
// item is renamed as "<original name> 1", incrementing the trailing number
// until an available file name is discovered.
const RENAME = 2;
// Replace behavior: replace the drive item if it already exists.
const REPLACE = 3;
}

View file

@ -0,0 +1,41 @@
<?php
namespace Krizalys\Onedrive;
class NameConflictBehaviorParameterizer
{
/**
* Parameterizes a given name conflict behavior.
*
* @param array $params The parameters.
* @param int $nameConflictBehavior The name conflict behavior.
*
* @return array
*
* @throws \Exception Thrown if the name conflict behavior given is not
* supported.
*/
public function parameterize(array $params, $nameConflictBehavior)
{
switch ($nameConflictBehavior) {
case NameConflictBehavior::FAIL:
$params['overwrite'] = 'false';
break;
case NameConflictBehavior::RENAME:
$params['overwrite'] = 'ChooseNewName';
break;
case NameConflictBehavior::REPLACE:
$params['overwrite'] = 'true';
break;
default:
throw new \Exception(
"Unsupported name conflict behavior: $nameConflictBehavior"
);
}
return $params;
}
}

View file

@ -0,0 +1,13 @@
<?php
namespace Krizalys\Onedrive;
class StreamBackEnd
{
// Memory-backed stream.
const MEMORY = 1;
// Temporary file-backed stream. A temporary file is actually used if the
// stream contents exceeds 2 MiB.
const TEMP = 2;
}

View file

@ -0,0 +1,30 @@
<?php
namespace Krizalys\Onedrive;
class StreamOpener
{
private static $uris = [
StreamBackEnd::MEMORY => 'php://memory',
StreamBackEnd::TEMP => 'php://temp',
];
/**
* Opens a stream given a stream back end.
*
* @param int $streamBackEnd The stream back end.
*
* @return bool|resource The open stream.
*
* @throws \Exception Thrown if the stream back end given is not supported.
*/
public function open($streamBackEnd)
{
if (!array_key_exists($streamBackEnd, self::$uris)) {
throw new \Exception("Unsupported stream back end: $streamBackEnd");
}
$uri = self::$uris[$streamBackEnd];
return fopen($uri, 'rw+b');
}
}