petr-borisenko/d7kit 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

petr-borisenko/d7kit

Composer 安装命令:

composer require petr-borisenko/d7kit

包简介

A comprehensive toolkit for working with Bitrix D7 framework, providing facades, repositories, and DTOs for streamlined development.

README 文档

README

A comprehensive toolkit for working with Bitrix D7 framework, providing facades, repositories, and native entities for streamlined development.

Overview

D7Kit is a modern PHP library that simplifies interaction with Bitrix CMS data structures. It provides clean abstractions for working with infoblocks, highload blocks, and property enumerations using contemporary PHP practices and the D7 framework.

The main components include:

  • Facades: Simplified interfaces for accessing Bitrix entities
  • Repositories: Data access layer implementations
  • Native Bitrix Entities: Direct access to Bitrix's entity objects for structured data exchange
  • Contracts: Interfaces defining clear contracts for implementations

Installation

composer require petr-borisenko/d7kit

Usage

Working with Infoblocks

Getting IBlock Facade Instance

The facade follows the singleton pattern, so you can get an instance using the instance() method:

<?php

use PetrBorisenko\D7Kit\Facade\IBlockFacade;

$iBlockFacade = IBlockFacade::instance('news'); // Replace 'news' with your infoblock code
$iBlock = $iBlockFacade->entity(); // Get the infoblock entity
$elementClass = $iBlockFacade->elementClass(); // Get the element class name
$sectionClass = $iBlockFacade->sectionClass(); // Get the section class name
$elementQuery = $iBlockFacade->elementQuery(); // Get element query builder
$sectionQuery = $iBlockFacade->sectionQuery(); // Get section query builder

Working with Highload Blocks

Getting Highload Block Facade Instance

Similar to infoblocks, you can get a facade instance for a highload block:

<?php

use PetrBorisenko\D7Kit\Facade\HighloadBlockFacade;

// Get facade instance by highload block name
$hlFacade = HighloadBlockFacade::instance('Cities'); // Replace 'Cities' with your highload block name
$hlBlock = $hlFacade->entity(); // Get the highload block entity
$entityClass = $hlFacade->elementClass(); // Get the entity class name for working with data
$elementQuery = $hlFacade->elementQuery(); // Get element query builder

Working with Property Enumerations

The toolkit provides methods to work with infoblock property enumerations:

Getting a Specific Enumeration Value

<?php

use PetrBorisenko\D7Kit\Facade\IBlockFacade;
use PetrBorisenko\D7Kit\Facade\PropertyEnumerationFacade;

$iBlockFacade = IBlockFacade::instance('news');
$enumFacade = new PropertyEnumerationFacade($iBlockFacade);

// Get a specific enumeration value by property code and case code (XML_ID)
try {
    $enumValue = $enumFacade->caseFrom('CATEGORY', 'TECHNOLOGY');
    echo "ID: {$enumValue->getId()}, XML_ID: {$enumValue->getXmlId()}, VALUE: {$enumValue->getValue()}";
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
}

Getting All Enumeration Values for a Property

<?php

use PetrBorisenko\D7Kit\Facade\IBlockFacade;
use PetrBorisenko\D7Kit\Facade\PropertyEnumerationFacade;

$iBlockFacade = IBlockFacade::instance('news');
$enumFacade = new PropertyEnumerationFacade($iBlockFacade);

// Get all enumeration values for a property
try {
    $enumList = $enumFacade->listFrom('CATEGORY');
    
    foreach ($enumList as $enumValue) {
        echo "ID: {$enumValue->getId()}, XML_ID: {$enumValue->getXmlId()}, VALUE: {$enumValue->getValue()}\n";
    }
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
}

Working with Entities

The toolkit returns native Bitrix entities for structured data handling:

<?php

// The facade methods return Bitrix entities directly
$enumValue = $enumFacade->caseFrom('CATEGORY', 'TECHNOLOGY');

// Access properties using getter methods
echo $enumValue->getId();      // Get the ID
echo $enumValue->getXmlId();   // Get the XML_ID
echo $enumValue->getValue();   // Get the value

// Work with collections
$enumList = $enumFacade->listFrom('CATEGORY');
foreach ($enumList as $enumValue) {
    echo $enumValue->getId() . ': ' . $enumValue->getValue() . "\n";
}

Complete Example

Here's a complete example showing how to use the toolkit in a service class:

<?php

declare(strict_types=1);

namespace App\Service;

use PetrBorisenko\D7Kit\Facade\IBlockFacade;
use PetrBorisenko\D7Kit\Facade\HighloadBlockFacade;
use PetrBorisenko\D7Kit\Facade\PropertyEnumerationFacade;

class NewsService
{
    private IBlockFacade $iBlockFacade;
    private PropertyEnumerationFacade $enumFacade;
    private HighloadBlockFacade $hlFacade;
    
    public function __construct()
    {
        $this->iBlockFacade = IBlockFacade::instance('news');
        $this->enumFacade = new PropertyEnumerationFacade($this->iBlockFacade);
        $this->hlFacade = HighloadBlockFacade::instance('Cities');
    }
    
    public function getCategoryById(string $categoryId): ?\Bitrix\Iblock\EO_PropertyEnumeration
    {
        try {
            return $this->enumFacade->caseFrom('CATEGORY', $categoryId);
        } catch (\Exception $e) {
            // Log error or handle as needed
            return null;
        }
    }
    
    public function getAllCategories(): ?\Bitrix\Iblock\EO_Property_Collection
    {
        try {
            return $this->enumFacade->listFrom('CATEGORY');
        } catch (\Exception $e) {
            // Log error or handle as needed
            return null;
        }
    }
    
    public function getNewsInfoblockId(): int
    {
        return $this->iBlockFacade->entity()->getId();
    }
    
    public function getCities(): array
    {
        // Get all cities from the highload block
        $entityClass = $this->hlFacade->elementClass();
        $hlData = $entityClass::query()
            ->setSelect(['ID', 'UF_NAME'])
            ->exec();
        
        $cities = [];
        while ($item = $hlData->fetch()) {
            $cities[] = ['ID' => $item['ID'], 'NAME' => $item['UF_NAME']];
        }
        return $cities;
    }
    
    public function getCityById(int $cityId): ?array
    {
        // Get a specific city by its ID
        $entityClass = $this->hlFacade->elementClass();
        $hlData = $entityClass::query()
            ->setSelect(['ID', 'UF_NAME'])
            ->where('ID', $cityId)
            ->setLimit(1)
            ->exec();
        
        return $hlData->fetch();
    }
}

Features

  • Modern PHP 8.2+ code with strict types
  • Facade pattern implementation for simplified access
  • Automatic loading of required Bitrix modules
  • Infoblock identification by code
  • Highload block identification by name
  • Compilation of infoblock and highload block entities for optimal performance
  • Methods for retrieving metadata (entities, class names, query builders)
  • Easy access to property enumerations
  • Proper error handling for common issues
  • Native Bitrix entities for structured data transfer
  • PSR-4 autoloading support

Architecture

The toolkit architecture consists of:

  • Contract namespace: Interfaces defining contracts for facades and entities
  • Facade namespace: Facade implementations for simplified access to Bitrix entities
  • Repository namespace: Data access layer implementations

Requirements

  • PHP 8.2+
  • Bitrix CMS
  • Bitrix "iblock" module enabled (for infoblock functionality)
  • Bitrix "highloadblock" module enabled (for highload block functionality)

Contributing

Feel free to submit issues and enhancement requests via GitHub.

License

This project is licensed under the MIT License.

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固