Skip to content

Commit

Permalink
Merge branch '1.x' into update-container-resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
repl6669 authored Sep 30, 2024
2 parents 347498a + 26d70d7 commit 370f920
Show file tree
Hide file tree
Showing 47 changed files with 191 additions and 81 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/fix-code-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4
with:
token: ${{ secrets.ACCESS_TOKEN }}
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ github.head_ref }}
- name: Set up PHP
uses: shivammathur/setup-php@v2
Expand All @@ -26,5 +26,8 @@ jobs:
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_user_name: "GitHub Action"
commit_user_email: "[email protected]"
commit_author: "Author <[email protected]>"
commit_message: >
chore: fix code style
11 changes: 1 addition & 10 deletions docs/core/reference/carts.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,6 @@ foreach ($cart->shippingBreakdown->items as $shippingBreakdown) {
}


foreach ($cart->discountBreakdown as $discountBreakdown) {
$discountBreakdown->discount_id
foreach ($discountBreakdown->lines as $discountLine) {
$discountLine->quantity
$discountLine->line
}
$discountBreakdown->total->value
}

foreach ($cart->discountBreakdown as $discountBreakdown) {
$discountBreakdown->discount_id
foreach ($discountBreakdown->lines as $discountLine) {
Expand Down Expand Up @@ -270,7 +261,7 @@ When you call current, you have two options, you either return `null` if they
don't have a cart, or you want to create one straight away. By default, we do
not create them initially as this could lead to a ton of cart models being
created for no good reason. If you want to enable this functionality, you can
adjust the config in `lunar/cart.php`
adjust the config in `lunar/cart_session.php`

### Forgetting the cart
Forgetting the cart will remove it from the user session and also soft-delete
Expand Down
2 changes: 1 addition & 1 deletion packages/admin/src/Filament/Resources/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public static function getBaseNameFormComponent(): Component
{
$nameType = Attribute::whereHandle('name')
->whereAttributeType(
(new (static::getModel()))->getMorphClass()
static::getModel()::morphName()
)
->first()?->type ?: TranslatedText::class;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public static function createRecord(array $data, string $model): Model
{
$currency = Currency::modelClass()::getDefault();

$nameAttribute = Attribute::modelClass()::whereAttributeType(
(new $model)->getMorphClass()
$nameAttribute = Attribute::whereAttributeType(
$model::morphName()
)
->whereHandle('name')
->first()
Expand Down
4 changes: 2 additions & 2 deletions packages/admin/src/Filament/Resources/ProductTypeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public static function getDefaultForm(Forms\Form $form): Forms\Form
Forms\Components\Tabs\Tab::make(__('lunarpanel::producttype.tabs.product_attributes.label'))
->schema([
AttributeSelector::make('mappedAttributes')
->withType((new (Product::modelClass()))->getMorphClass())
->withType(Product::morphName())
->relationship(name: 'mappedAttributes')
->label('')
->columnSpan(2),
]),
Forms\Components\Tabs\Tab::make(__('lunarpanel::producttype.tabs.variant_attributes.label'))
->schema([
AttributeSelector::make('mappedAttributes')
->withType((new (ProductVariant::modelClass()))->getMorphClass())
->withType(ProductVariant::morphName())
->relationship(name: 'mappedAttributes')
->label('')
->columnSpan(2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function setUp(): void
$this->success();
});

$attribute = Attribute::modelClass()::where('attribute_type', '=', (new Collection)->getMorphClass())
$attribute = Attribute::where('attribute_type', '=', Collection::morphName())
->where('handle', '=', 'name')->first();

$formInput = TextInput::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function setUp(): void

$record = $this->process(function (array $data) {
$attribute = Attribute::whereHandle('name')->whereAttributeType(
(new Collection)->getMorphClass()
Collection::morphName()
)->first()->type;

return Collection::create([
Expand Down Expand Up @@ -58,7 +58,7 @@ public function setUp(): void
$this->success();
});

$attribute = Attribute::where('attribute_type', '=', (new Collection)->getMorphClass())
$attribute = Attribute::where('attribute_type', '=', Collection::morphName())
->where('handle', '=', 'name')->first();

$formInput = TextInput::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function createChildCollection(CollectionContract $parent, array|string $
{
DB::beginTransaction();

$attribute = Attribute::modelClass()::whereHandle('name')->whereAttributeType(
(new Collection)->getMorphClass()
$attribute = Attribute::whereHandle('name')->whereAttributeType(
Collection::morphName()
)->first()->type;

$parent->appendNode(Collection::modelClass()::create([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public function getAttributeGroups()
$this->getRelationship()->getParent()
);

if ($type === (new ProductType)->getMorphClass()) {
$type = (new Product)->getMorphClass();
if ($type === ProductType::morphName()) {
$type = Product::morphName();
}

if ($this->attributableType) {
Expand Down
6 changes: 3 additions & 3 deletions packages/admin/src/Support/Forms/Components/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ protected function setUp(): void

$productTypeId = null;

$morphMap = (new $modelClass)->getMorphClass();
$morphMap = $modelClass::morphName();

$attributeQuery = Attribute::modelClass()::where('attribute_type', $morphMap);

// Products are unique in that they use product types to map attributes, so we need
// to try and find the product type ID
if ($morphMap == (new Product)->getMorphClass()) {
if ($morphMap == Product::morphName()) {
$productTypeId = $record?->product_type_id ?: ProductType::first()->id;

// If we have a product type, the attributes should be based off that.
Expand All @@ -41,7 +41,7 @@ protected function setUp(): void
}
}

if ($morphMap == (new ProductVariant)->getMorphClass()) {
if ($morphMap == ProductVariant::morphName()) {
$productTypeId = $record?->product?->product_type_id ?: ProductType::first()->id;
// If we have a product type, the attributes should be based off that.
if ($productTypeId) {
Expand Down
2 changes: 1 addition & 1 deletion packages/admin/src/Support/Resources/BaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected static function applyGlobalSearchAttributeConstraints(Builder $query,
protected static function mapSearchableAttributes(array &$map)
{
$attributes = Attribute::whereAttributeType(
(new (static::getModel()))->getMorphClass()
static::getModel()::morphName()
)
->whereSearchable(true)
->get();
Expand Down
2 changes: 1 addition & 1 deletion packages/admin/src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function get_search_builder($model, $search): Laravel\Scout\Builder|Builder

foreach (explode(' ', $search) as $searchWord) {
$query->where(function (Builder $query) use ($model, $searchWord) {
$attributes = Attribute::whereAttributeType($model->getMorphClass())
$attributes = Attribute::whereAttributeType($model::morphName())
->whereSearchable(true)
->get();

Expand Down
2 changes: 1 addition & 1 deletion packages/core/database/factories/CartLineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function definition(): array
return [
'cart_id' => Cart::factory(),
'quantity' => $this->faker->numberBetween(0, 1000),
'purchasable_type' => (new ProductVariant)->getMorphClass(),
'purchasable_type' => ProductVariant::morphName(),
'purchasable_id' => ProductVariant::factory(),
'meta' => null,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function definition(): array
{
return [
'purchasable_id' => ProductVariant::factory(),
'purchasable_type' => (new ProductVariant)->getMorphClass(),
'purchasable_type' => ProductVariant::morphName(),
];
}
}
2 changes: 1 addition & 1 deletion packages/core/database/factories/OrderLineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function definition(): array
{
return [
'order_id' => Order::factory(),
'purchasable_type' => (new ProductVariant)->getMorphClass(),
'purchasable_type' => ProductVariant::morphName(),
'purchasable_id' => ProductVariant::factory(),
'type' => 'physical',
'description' => $this->faker->sentence,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/database/factories/UrlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function definition(): array
'slug' => $this->faker->slug,
'default' => true,
'language_id' => Language::factory(),
'element_type' => (new Product)->getMorphClass(),
'element_type' => Product::morphName(),
'element_id' => 1,
];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,56 @@
<?php

use Illuminate\Support\Facades\Schema;
use Lunar\Base\BaseModel;
use Lunar\Base\Migration;
use Spatie\StructureDiscoverer\Discover;

class RemapPolymorphicRelations extends Migration
{
public function up()
{
$modelClasses = collect(
Discover::in(__DIR__.'/../../src/Models')
->classes()
->extending(BaseModel::class)
->get()
)->mapWithKeys(
$modelClasses = collect([
\Lunar\Models\CartLine::class,
\Lunar\Models\ProductOption::class,
\Lunar\Models\Asset::class,
\Lunar\Models\Brand::class,
\Lunar\Models\TaxZone::class,
\Lunar\Models\TaxZoneCountry::class,
\Lunar\Models\TaxZoneCustomerGroup::class,
\Lunar\Models\DiscountCollection::class,
\Lunar\Models\TaxClass::class,
\Lunar\Models\ProductOptionValue::class,
\Lunar\Models\Channel::class,
\Lunar\Models\AttributeGroup::class,
\Lunar\Models\Tag::class,
\Lunar\Models\Cart::class,
\Lunar\Models\Collection::class,
\Lunar\Models\Discount::class,
\Lunar\Models\TaxRate::class,
\Lunar\Models\Price::class,
\Lunar\Models\DiscountPurchasable::class,
\Lunar\Models\State::class,
\Lunar\Models\UserPermission::class,
\Lunar\Models\OrderAddress::class,
\Lunar\Models\Country::class,
\Lunar\Models\Address::class,
\Lunar\Models\Url::class,
\Lunar\Models\ProductVariant::class,
\Lunar\Models\TaxZonePostcode::class,
\Lunar\Models\ProductAssociation::class,
\Lunar\Models\TaxRateAmount::class,
\Lunar\Models\Attribute::class,
\Lunar\Models\Order::class,
\Lunar\Models\Customer::class,
\Lunar\Models\OrderLine::class,
\Lunar\Models\CartAddress::class,
\Lunar\Models\Language::class,
\Lunar\Models\TaxZoneState::class,
\Lunar\Models\Currency::class,
\Lunar\Models\Product::class,
\Lunar\Models\Transaction::class,
\Lunar\Models\ProductType::class,
\Lunar\Models\CollectionGroup::class,
\Lunar\Models\CustomerGroup::class,
])->mapWithKeys(
fn ($class) => [
$class => \Lunar\Facades\ModelManifest::getMorphMapKey($class),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public function run()

DB::table("{$prefix}attributes")
->whereAttributeType(
(new ProductType)->getMorphClass()
ProductType::morphName()
)
->update([
'attribute_type' => 'product',
]);

DB::table("{$prefix}attribute_groups")
->whereAttributableType(
(new ProductType)->getMorphClass()
ProductType::morphName()
)
->update([
'attributable_type' => 'product',
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Base/Traits/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getAttributableClassnameAttribute()

public function getAttributableMorphMapAttribute()
{
return (new self)->getMorphClass();
return self::morphName();
}

/**
Expand Down
59 changes: 55 additions & 4 deletions packages/core/src/Base/Traits/HasModelExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,71 @@
namespace Lunar\Base\Traits;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Lunar\Base\BaseModel;
use Lunar\Facades\ModelManifest;

trait HasModelExtending
{
public function newModelQuery(): Builder
{
$realClass = static::modelClass();
$concreteClass = static::modelClass();
$parentClass = get_parent_class($concreteClass);

// If they are both the same class i.e. they haven't changed
// then just call the parent method.
if ($this instanceof $realClass) {
if ($parentClass == BaseModel::class || $this instanceof $concreteClass) {
return parent::newModelQuery();
}

return $this->newEloquentBuilder(
$this->newBaseQueryBuilder()
)->setModel(new $realClass($this->toArray()));
)->setModel(
static::withoutEvents(
fn () => $this->replicateInto($concreteClass)
)
);
}

public function replicateInto($newClass)
{
$defaults = array_values(array_filter([
$this->getKeyName(),
$this->getCreatedAtColumn(),
$this->getUpdatedAtColumn(),
...$this->uniqueIds(),
'laravel_through_key',
]));

$attributes = Arr::except(
$this->getAttributes(), $defaults
);

return tap(new $newClass, function ($instance) use ($attributes): Model {
$instance->setRawAttributes($attributes);

$instance->setRelations($this->relations);

return $instance;
});
}

public function getForeignKey(): string
{
$parentClass = get_parent_class($this);

return $parentClass == BaseModel::class ? parent::getForeignKey() : Str::snake(class_basename($parentClass)).'_'.$this->getKeyName();

}

public function getTable()
{
$parentClass = get_parent_class($this);

return $parentClass == BaseModel::class ? parent::getTable() : (new $parentClass)->table;
}

public static function __callStatic($method, $parameters)
Expand All @@ -46,8 +92,13 @@ public static function modelClass(): string
}

/**
* Returns the morph class for a model class registered in the model manifest.
* Returns the model alias registered in the model relation morph map.
*/
public static function morphName(): string
{
return (new (static::modelClass()))->getMorphClass();
}

public function getMorphClass(): string
{
$morphMap = Relation::morphMap();
Expand Down
Loading

0 comments on commit 370f920

Please sign in to comment.