2.4k sao GitHub và vẫn tăng — aimeos/macro là dự án PHP mà TopGit đang theo dõi trên nền tảng. Customize code using closures
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
The PHP Macro package offers closure (anonymous function) based setter dependency
injection by providing a trait which can be included into any class.
composer req aimeos/macro
This package is for application, framework and library developers who want to
allow customizing the behavior of their code by their users.
Why macros
In applications, frameworks or libraries which are build for customization it’s
necessary to allow overwriting existing functionality to be able customize its
behavior. This is where macros are very handy because they can add custom code
using closures.
With the PHP Macro package, you can also allow users to overwrite methods in
base classes without forcing your users to extend these classes. The PHP Macro
package uses NO reflection or other hacks, just pure PHP methods.
There are some pros and cons when compared to class based depencency injection:
Pro:
Less code to write and much easier to implement for simple stuff
Custom closures can be inherited and overwritten like class methods
Con:
Limited static code analysis possibilities
Anonymous function can not be forced to implement an interface
Thus, it's not a replacement for class based depencency injection but a lightweight
addition for small extension points where full-blown dependency injection using
classes implementing interfaces are too much work.
Allow customization
The result of existing methods can be modified if the original method checks
for an existing macro and use that instead its own implementation:
// original code
class Order
{
use Aimeos\Macro\Macroable;
private $id = '123';
public function getOrderNumber()
{
$fcn = static::macro( 'orderNumber' );
return $fcn ? $fcn( $this->id ) : $this->id;
}
};
Now, you can add your custom orderNumber macro that will be used instead:
// user code
Order::macro( 'orderNumber', function( string $id ) {
return date( 'Y' ) . '-' . $id;
} );
(new Order)->getOrderNumber(); // now returns '2020-123'
Thus, you can generate own output or pass a different result to subseqent methods
within the application.
Access class properties
When macros are called in an object context, they can also access class properties:
// original code
class A
{
use Aimeos\Macro\Macroable;
private $name = 'A';
};
Here, the private property $name is available in the macro:
The macro can use the property as input for creating the returned value.
Use inherited macros
The PHP macro package also allows to inherit macros from parent classes. Then,
they can access class properties of the child class just like regular class
methods:
// original code
class A
{
use Aimeos\Macro\Macroable;
private $name = 'A';
};
class B extends A
{
private $name = 'B';
};
Macros added to the parent class will be available in child classes too:
Class B extends from class A but provides a different $name property. The
macro inherited from class A will now use the property of class B.
Overwrite inherited macros
It's also possible to overwrite macros inherited from parent classes as it's
possible with regular class methods:
// original code
class A
{
use Aimeos\Macro\Macroable;
public function do() {
return static::macro( 'concat' )( [1, 2, 3] );
}
};
class B extends A {};
class C extends A {};
Now you can add macros to the parent class and one of the child classes:
This enables you to add special handling for single classes even if all other
classes still use the macro added to class A.
Overwrite protected methods
Base classes often offer a set of methods that are used by the child classes.
In PHP, replacing the methods of a base class is impossible and thus, you have
to overwrite each child class with your own implementation.
To avoid that, the original method can use the call() method instead of calling
the method of the parent class directly:
// original code
class A
{
use Aimeos\Macro\Macroable;
protected function getName( $prefix )
{
return $prefix . 'A';
}
};
class B extends A
{
public function do()
{
return $this->call( 'getName', 'B-' );
}
};
This will check if there's a macro getName available and will call that instead
of the getName() method:
// user code
(new B)->do(); // returns 'B-A'
A::macro( 'getName', function( $prefix ) {
return $this->getName( $prefix ) . '-123';
} );
(new B)->do(); // returns 'B-A-123'
The original getName() method can still be used in the macro.
Reset macros
Sometimes, it may be necessary to remove macros from objects, especially when
running automated tests. You can unset a macro by using:
class A
{
use Aimeos\Macro\Macroable;
};
// add macro
A::macro( 'test', function() {
return 'test';
} );
// remove macro
A::unmacro( 'test' );
aimeos/macro có 2.4k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/aimeos/macro. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
aimeos/macro có phải mã nguồn mở không?
Có — aimeos/macro phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/aimeos/macro.
aimeos/macro còn đang phát triển không?
Commit gần nhất trên aimeos/macro là 10 tháng trước (theo timestamp GitHub). Repo có 1 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
aimeos/macro dùng license gì?
aimeos/macro phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
aimeos/macro là gì?
aimeos/macro (aimeos/macro) là dự án PHP trên GitHub. Theo mô tả gốc: Customize code using closures
aimeos/macro viết bằng ngôn ngữ gì?
aimeos/macro chủ yếu viết bằng PHP. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Cùng nhóm Backend còn repo nào?
aimeos/macro thuộc nhóm Backend trên TopGit, cùng 10 topic GitHub. Trang Trending và Topics liệt kê các repo cùng số sao và cùng ngôn ngữ để so sánh.
Đọc thêm về aimeos/macro ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/aimeos/macro là nguồn chính thức.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về macro?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về macro.