Commit 3abf4283 authored by DESKTOP-J74Q997\abc's avatar DESKTOP-J74Q997\abc
Browse files

新增shoplazza 工具包

parent 5d7f10d5
Loading
Loading
Loading
Loading
+168 −0
Original line number Diff line number Diff line
<?php
/**
 * Created by PhpStorm.
 * User: Zero
 * Date: 2020/8/18
 * Time: 8:13
 */

namespace Meibuyu\Micro\Shoplazza;

use Exception;
use Meibuyu\Micro\Shopify\lib\AbstractShopify;
use Meibuyu\Micro\Shopify\lib\Collect;
use Meibuyu\Micro\Shopify\lib\Collection;
use Meibuyu\Micro\Shopify\lib\CustomCollection;
use Meibuyu\Micro\Shopify\lib\Event;
use Meibuyu\Micro\Shopify\lib\Fulfillment;
use Meibuyu\Micro\Shopify\lib\FulfillmentOrder;
use Meibuyu\Micro\Shopify\lib\FulfillmentService;
use Meibuyu\Micro\Shopify\lib\GraphQL;
use Meibuyu\Micro\Shopify\lib\InventoryItem;
use Meibuyu\Micro\Shopify\lib\InventoryLevel;
use Meibuyu\Micro\Shopify\lib\Location;
use Meibuyu\Micro\Shopify\lib\Metafield;
use Meibuyu\Micro\Shopify\lib\Order;
use Meibuyu\Micro\Shopify\lib\Product;
use Meibuyu\Micro\Shopify\lib\ProductVariant;
use Meibuyu\Micro\Shopify\lib\SmartCollection;
use Meibuyu\Micro\Shopify\lib\Webhook;

/**
 * Class ShoplazzaApp
 * @package Meibuyu\Micro\Shoplazza
 *
 * @property-read Webhook $Webhook
 * @property-read Collect $Collect
 * @property-read Collection $Collection
 * @property-read CustomCollection $CustomCollection
 * @property-read SmartCollection $SmartCollection
 * @property-read Metafield $Metafield
 * @property-read Product $Product
 * @property-read ProductVariant $ProductVariant
 * @property-read InventoryItem $InventoryItem
 * @property-read InventoryLevel $InventoryLevel
 * @property-read Location $Location
 * @property-read Order $Order
 * @property-read Event $Event
 * @property-read Fulfillment $Fulfillment
 * @property-read FulfillmentService $FulfillmentService
 * @property-read GraphQL $GraphQL
 *
 * @method Webhook Webhook(integer $id = null)
 * @method Collection Collection(integer $id = null)
 * @method CustomCollection CustomCollection(integer $id = null)
 * @method SmartCollection SmartCollection(integer $id = null)
 * @method Metafield Metafield(integer $id = null)
 * @method Product Product(integer $id = null)
 * @method ProductVariant ProductVariant(integer $id = null)
 * @method InventoryItem InventoryItem(integer $id = null)
 * @method InventoryLevel InventoryLevel(integer $id = null)
 * @method Location Location(integer $id = null)
 * @method Order Order(integer $id = null)
 * @method Event Event(integer $id = null)
 * @method Fulfillment Fulfillment(integer $id = null)
 * @method FulfillmentOrder FulfillmentOrder()
 * @method FulfillmentService FulfillmentService(integer $id = null)
 * @method GraphQL GraphQL()
 *
 */
class ShoplazzaApp
{

    protected $resources = [
        'Webhook',
        'Collect',
        'Collection',
        'CustomCollection',
        'SmartCollection',
        'Metafield',
        'Product',
        'ProductVariant',
        'InventoryItem',
        'InventoryLevel',
        'Location',
        'Order',
        'Event',
        'Fulfillment',
        'FulfillmentOrder',
        'FulfillmentService',
        'GraphQL',
    ];

    protected $childResources = array(
        'Fulfillment' => 'Order',
        'FulfillmentEvent' => 'Fulfillment',
        'FulfillmentOrder' => 'Order',
        'OrderRisk' => 'Order',
        'ProductImage' => 'Product',
        'ProductVariant' => 'Product',
        'DiscountCode' => 'PriceRule',
        'Refund' => 'Order',
        'Transaction' => 'Order',
    );

    public $config = [];

    public $defaultApiVersion = '2022-01';

    /**
     * ShopifyApp constructor.
     * @param array $config
     */
    public function __construct($config)
    {
        $this->config = [
            'api_version' => $this->defaultApiVersion
        ];
        foreach ($config as $key => $value) {
            $this->config[$key] = $value;
        }
        if (isset($config['shop_url'])) {
            $this->setApiUrl();
        }
    }

    /**
     * 返回AbstractShopify实例
     * @param string $className 实现的类名
     * @return AbstractShopify
     * @throws Exception
     */
    public function __get($className)
    {
        return $this->$className();
    }

    /**
     * 返回AbstractShopify实例
     * @param string $className 实现的类名
     * @param $arguments
     * @return AbstractShopify
     * @throws Exception
     */
    public function __call($className, $arguments)
    {
        if (!in_array($className, $this->resources)) {
            if (isset($this->childResources[$className])) {
                $message = "$className 是属于 {$this->childResources[$className]} 的子集, 无法直接访问";
            } else {
                $message = "未知类 $className";
            }
            throw new Exception($message);
        }

        $resourceID = !empty($arguments) ? $arguments[0] : null;
        $resourceClassName = __NAMESPACE__ . "\\lib\\$className";
        return new $resourceClassName($this->config, $resourceID);
    }

    public function setApiUrl()
    {
        $shopUrl = $this->config['shop_url'];
        $shopUrl = preg_replace('#^https?://|/$#', '', $shopUrl);
        $apiVersion = $this->config['api_version'];
        $this->config['api_url'] = "https://$shopUrl/openapi/$apiVersion/";
    }

}
+29 −0
Original line number Diff line number Diff line
<?php

namespace Meibuyu\Micro\Shoplazza;

use Hyperf\Contract\ContainerInterface;

class ShoplazzaFactory
{

    /**
     * @var ContainerInterface
     */
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function create(array $config = []): ShoplazzaApp
    {
        if (method_exists($this->container, 'make')) {
            // Create by DI for AOP.
            return $this->container->make(ShoplazzaApp::class, ['config' => $config]);
        }
        return new ShoplazzaApp($config);
    }

}
+355 −0
Original line number Diff line number Diff line
<?php
/**
 * Created by PhpStorm.
 * User: Zero
 * Date: 2020/8/18
 * Time: 8:18
 */

namespace Meibuyu\Micro\Shoplazza\lib;

use Exception;
use Meibuyu\Micro\Shoplazza\tools\CurlHttpRequestJson;
use Meibuyu\Micro\Shoplazza\tools\HttpRequestJson;
use Psr\Http\Message\ResponseInterface;

abstract class AbstractShopify
{

    /**
     * 资源id
     * @var int
     */
    public $id = null;

    protected $httpHeaders = [];

    protected $resourceUrl;
    protected $resourceKey;
    protected $pluralizeKey;

    /**
     * 无count方法
     * @var boolean
     */
    public $countEnabled = true;

    // 子集资源
    protected $childResource = [];

    // 自定义请求方法
    protected $customGetActions = [];
    protected $customPostActions = [];
    protected $customPutActions = [];
    protected $customDeleteActions = [];

    private $config;

    /**
     * @var HttpRequestJson|CurlHttpRequestJson
     */
    private $httpRequestJson;

    /**
     * AbstractShopify constructor.
     * @param $config
     * @param null $id
     * @param string $parentResourceUrl
     * @throws Exception
     */
    public function __construct($config, $id = null, $parentResourceUrl = null)
    {
        $this->config = $config;
        $this->id = $id;
        $this->pluralizeKey = $this->pluralizeKey ?: $this->resourceKey . 's';
        $this->resourceUrl = ($parentResourceUrl ? "$parentResourceUrl/" : $config['api_url']) . $this->pluralizeKey . ($this->id ? "/{$this->id}" : '');
        $this->httpRequestJson = make(CurlHttpRequestJson::class);

        if (isset($config['access_token'])) {
            $this->httpHeaders['access-token'] = $config['access_token'];
        } elseif (!isset($config['access_token'])) {
            throw new Exception("请设置access_token值");
        }

    }

    /**
     * 调用子集资源
     * @param $childName
     * @return mixed
     */
    public function __get($childName)
    {
        return $this->$childName();
    }

    /**
     * 调用自定义方法
     * @param $name
     * @param $arguments
     * @return mixed
     * @throws Exception
     */
    public function __call($name, $arguments)
    {
        if (ctype_upper($name[0])) {
            $childKey = array_search($name, $this->childResource);
            if ($childKey === false) {
                throw new Exception(" $name 不属于 {$this->getResourceName()}");
            }
            $childClassName = !is_numeric($childKey) ? $childKey : $name;
            $childClass = __NAMESPACE__ . "\\" . $childClassName;
            $resourceID = !empty($arguments) ? $arguments[0] : null;
            return new $childClass($this->config, $resourceID, $this->resourceUrl);
        } else {
            $actionMaps = [
                'post' => 'customPostActions',
                'put' => 'customPutActions',
                'get' => 'customGetActions',
                'delete' => 'customDeleteActions',
            ];
            //Get the array key for the action in the actions array
            foreach ($actionMaps as $httpMethod => $actionArrayKey) {
                $actionKey = array_search($name, $this->$actionArrayKey);
                if ($actionKey !== false) break;
            }
            if ($actionKey === false) {
                throw new Exception("No action named $name is defined for " . $this->getResourceName());
            }
            //If any associative key is given to the action, then it will be considered as the method name,
            //otherwise the action name will be the method name
            $customAction = !is_numeric($actionKey) ? $actionKey : $name;
            //Get the first argument if provided with the method call
            $methodArgument = !empty($arguments) ? $arguments[0] : [];
            //Url parameters
            $urlParams = [];
            //Data body
            $dataArray = [];
            //Consider the argument as url parameters for get and delete request
            //and data array for post and put request
            if ($httpMethod == 'post' || $httpMethod == 'put') {
                $dataArray = $methodArgument;
            } else {
                $urlParams = $methodArgument;
            }
            $url = $this->generateUrl($urlParams, null, $customAction);
            if ($httpMethod == 'post' || $httpMethod == 'put') {
                return $this->$httpMethod($dataArray, $url, false);
            } else {
                return $this->$httpMethod($dataArray, $url);
            }
        }
    }

    private function getResourceName()
    {
        return substr(get_called_class(), strrpos(get_called_class(), '\\') + 1);
    }

    public function generateUrl($urlParams = [], $id = null, $customAction = null)
    {
        $resourceUrl = $this->resourceUrl;
        if ($id) {
            if ($this->id) {
                if ($id !== $this->id) {
                    $resourceUrl = str_replace($this->id, $id, $this->resourceUrl);
                }
            } else {
                $resourceUrl = $this->resourceUrl . "/$id";
            }
        }
        return $resourceUrl . ($customAction ? "/$customAction" : '') . (!empty($urlParams) ? '?' . http_build_query($urlParams) : '');
    }

    /**
     * 获取数量
     * @param array $urlParams
     * @return mixed
     * @throws Exception
     */
    public function count($urlParams = [])
    {
        if (!$this->countEnabled) {
            throw new Exception("当前类{$this->getResourceName()}不支持count()方法");
        }
        $url = $this->generateUrl($urlParams, null, 'count');
        return $this->get([], $url, 'count');
    }

    /**
     * @param array $urlParams
     * @param null $url
     * @param null $dataKey
     * @return mixed
     * @throws Exception
     */
    public function get($urlParams = [], $url = null, $dataKey = null)
    {
        if (!$url) $url = $this->generateUrl($urlParams);
        $response = $this->httpRequestJson->get($url, $this->httpHeaders);
        if (!$dataKey) $dataKey = $this->id ? $this->resourceKey : $this->pluralizeKey;
        return $this->processResponse($response, $dataKey);
    }

    /**
     * 分页
     * @param null $url
     * @param array $urlParams
     * @return mixed ['data' => [数据], 'next_link' => '下一页链接']
     * @throws Exception
     */
    public function page($url = null, $urlParams = [])
    {
        if (!$url) $url = $this->generateUrl($urlParams);
        $response = $this->httpRequestJson->get($url, $this->httpHeaders);
        return $this->processPageResponse($response, $this->pluralizeKey);
    }

    /**
     * 根据id获取一条数据
     * @param $id
     * @param array $urlParams
     * @return mixed
     * @throws Exception
     */
    public function show($id, $urlParams = [])
    {
        $url = $this->generateUrl($urlParams, $id);
        $response = $this->httpRequestJson->get($url, $this->httpHeaders);
        return $this->processResponse($response, $this->resourceKey);
    }

    /**
     * @param $dataArray
     * @param null $url
     * @param bool $wrapData
     * @return mixed
     * @throws Exception
     */
    public function post($dataArray, $url = null, $wrapData = true)
    {
        if (!$url) $url = $this->generateUrl();
        if ($wrapData && !empty($dataArray)) $dataArray = $this->wrapData($dataArray);
        $response = $this->httpRequestJson->post($url, $dataArray, $this->httpHeaders);
        return $this->processResponse($response, $this->resourceKey);
    }

    /**
     * @param int|string $id
     * @param $dataArray
     * @param null $url
     * @param bool $wrapData
     * @return mixed
     * @throws Exception
     */
    public function put($id, $dataArray, $url = null, $wrapData = true)
    {
        if (!$url) $url = $this->generateUrl([], $id);
        if ($wrapData && !empty($dataArray)) $dataArray = $this->wrapData($dataArray);
        $response = $this->httpRequestJson->put($url, $dataArray, $this->httpHeaders);
        return $this->processResponse($response, $this->resourceKey);
    }

    /**
     * @param int|string $id
     * @param array $urlParams
     * @param null $url
     * @return mixed
     * @throws Exception
     */
    public function delete($id = null, $urlParams = [], $url = null)
    {
        if (!$url) $url = $this->generateUrl($urlParams, $id);
        $response = $this->httpRequestJson->delete($url, $this->httpHeaders);
        return $this->processResponse($response);
    }

    protected function wrapData($dataArray, $dataKey = null)
    {
        if (!$dataKey) $dataKey = $this->resourceKey;
        return [$dataKey => $dataArray];
    }

    protected function castString($array)
    {
        if (!is_array($array)) return (string)$array;
        $string = '';
        $i = 0;
        foreach ($array as $key => $val) {
            $string .= ($i === $key ? '' : "$key - ") . $this->castString($val) . ', ';
            $i++;
        }
        $string = rtrim($string, ', ');
        return $string;
    }

    /**
     * 处理响应
     * @param array $response
     * @param null $dataKey
     * @return mixed
     * @throws Exception
     */
    public function processResponse($response, $dataKey = null)
    {
        [$code, , $content] = $response;
        $content = json_decode($content, true);
        if (isset($content['errors'])) {
            throw new Exception($this->castString($content['errors']), $code);
        }
        if ($dataKey && isset($content[$dataKey])) {
            return $content[$dataKey];
        } else {
            return $content;
        }
    }

    /**
     * 处理响应
     * @param ResponseInterface $response
     * @param null $dataKey
     * @return mixed
     * @throws Exception
     */
    public function processPageResponse($response, $dataKey = null)
    {
        [$code, $headers, $content] = $response;
        $content = json_decode($content, true);
        $link = $this->getLink($headers);
        if (isset($content['errors'])) {
            throw new Exception($this->castString($content['errors']), $code);
        }
        if ($dataKey && isset($content[$dataKey])) {
            $data = $content[$dataKey];
        } else {
            $data = $content;
        }
        return ['data' => $data, 'next_link' => $link];
    }

    public function getLink($header, $type = 'next')
    {
        if (!empty($header['x-shopify-api-version'][0]) && $header['x-shopify-api-version'][0] < '2019-07') {
            return null;
        }
        if (!empty($header['link'][0])) {
            $headerLinks = $header['link'][0];
            if (stristr($headerLinks, '; rel="' . $type . '"') > -1) {
                $headerLinks = explode(',', $headerLinks);
                foreach ($headerLinks as $headerLink) {
                    if (stristr($headerLink, '; rel="' . $type . '"') === -1) {
                        continue;
                    }

                    $pattern = '#<(.*?)>; rel="' . $type . '"#m';
                    preg_match($pattern, $headerLink, $linkResponseHeaders);
                    if ($linkResponseHeaders) {
                        return $linkResponseHeaders[1];
                    }
                }
            }
        }
        return null;
    }

}
+16 −0
Original line number Diff line number Diff line
<?php
/**
 * Created by PhpStorm.
 * User: Zero
 * Date: 2020/8/24
 * Time: 16:50
 */

namespace Meibuyu\Micro\Shoplazza\lib;

class Collect extends AbstractShopify
{

    protected $resourceKey = 'collect';

}
+28 −0
Original line number Diff line number Diff line
<?php
/**
 * Created by PhpStorm.
 * User: Zero
 * Date: 2020/8/24
 * Time: 16:50
 */

namespace Meibuyu\Micro\Shoplazza\lib;

/**
 * Class Collection
 * @package Meibuyu\Micro\Shoplazza\lib
 *
 * @property-read Metafield $Metafield
 *
 * @method Metafield Metafield(integer $id = null)
 */
class Collection extends AbstractShopify
{

    protected $resourceKey = 'collection';

    protected $childResource = [
        'Metafield',
    ];

}
Loading