PHP code example of imrev-agency / exchange1c

1. Go to this page and download the library: Download imrev-agency/exchange1c library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

imrev-agency / exchange1c example snippets


$configValues = [
    'import_dir' => storage_path('1c_exchange'),
    'auth' => [
        'login'    => 'admin',
        'password' => 'secret',
        'custom'   => false,
    ],
    'use_zip'    => false,
    'file_part'  => 0,
    'models'     => [
        \Bigperson\Exchange1C\Interfaces\GroupInterface::class   => \App\Models\Category::class,
        \Bigperson\Exchange1C\Interfaces\ProductInterface::class => \App\Models\Product::class,
        \Bigperson\Exchange1C\Interfaces\OfferInterface::class   => \App\Models\Offer::class,
    ],
];
$config = new \Bigperson\Exchange1C\Config($configValues);

app()->bind(\Bigperson\Exchange1C\Config::class, fn() => $config);

$catalogService = app(\Bigperson\Exchange1C\Services\CatalogService::class);

$this->app->bind(
    \Bigperson\Exchange1C\Interfaces\AuthServiceInterface::class,
    \App\Services\MyCustomAuthService::class
);



$configValues = [
    'import_dir' => '1c_exchange',
    'auth' => [
        'login'    => 'admin',
        'password' => 'admin',
        'custom'   => false,
    ],
    'use_zip'    => false,
    'file_part'  => 0,
    'models'     => [
        \Bigperson\Exchange1C\Interfaces\GroupInterface::class   => \App\Models\Category::class,
        \Bigperson\Exchange1C\Interfaces\ProductInterface::class => \App\Models\Product::class,
        \Bigperson\Exchange1C\Interfaces\OfferInterface::class   => \App\Models\Offer::class,
    ],
];
$config      = new \Bigperson\Exchange1C\Config($configValues);
$request     = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
$dispatcher  = new \Your\EventDispatcher\Implementation(); // реалізує EventDispatcherInterface
$modelBuilder = new \Bigperson\Exchange1C\ModelBuilder();

$authService     = new \Bigperson\Exchange1C\Services\AuthService($config);
$loaderService   = new \Bigperson\Exchange1C\Services\FileLoaderService($config);
$categoryService = new \Bigperson\Exchange1C\Services\CategoryService($config, $dispatcher, $modelBuilder);
$offerService    = new \Bigperson\Exchange1C\Services\OfferService($config, $dispatcher, $modelBuilder);
$catalogService  = new \Bigperson\Exchange1C\Services\CatalogService($config, $authService, $loaderService, $categoryService, $offerService);

$mode = $request->get('mode');
$type = $request->get('type');

try {
    if ($type === 'catalog') {
        if (!method_exists($catalogService, $mode)) {
            throw new \Exception('not correct request, mode=' . $mode);
        }
        $body     = $catalogService->$mode($request);
        $response = new \Symfony\Component\HttpFoundation\Response($body, 200, ['Content-Type', 'text/plain']);
        $response->send();
    } else {
        throw new \LogicException(sprintf('Logic for type "%s" not implemented', $type));
    }
} catch (\Exception $e) {
    $body  = "failure\n";
    $body .= $e->getMessage() . "\n";
    $body .= $e->getFile() . "\n";
    $body .= $e->getLine() . "\n";

    $response = new \Symfony\Component\HttpFoundation\Response($body, 500, ['Content-Type', 'text/plain']);
    $response->send();
}