- DB 연결: 로컬/Docker 환경 오버라이딩 설정 (.env) - 테넌트 위젯: redirect 버그 수정 (TenantSelectorWidget) - 통계 위젯: 사용자/제품/자재/주문 카드 추가 (StatsOverviewWidget) - 리소스 한국어화: Product, Material 모델 레이블 추가 - 대시보드: 위젯 등록 및 캐시 최적화 🤖 Generated with [Claude Code](https://claude.ai/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
122 lines
3.6 KiB
PHP
122 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Design;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use App\Traits\BelongsToTenant;
|
|
|
|
class ModelFormula extends Model
|
|
{
|
|
use SoftDeletes, BelongsToTenant;
|
|
|
|
protected $table = 'model_formulas';
|
|
|
|
protected $fillable = [
|
|
'tenant_id',
|
|
'model_id',
|
|
'formula_name',
|
|
'formula_expression',
|
|
'unit',
|
|
'description',
|
|
'calculation_order',
|
|
'dependencies',
|
|
];
|
|
|
|
protected $casts = [
|
|
'calculation_order' => 'integer',
|
|
'dependencies' => 'array',
|
|
];
|
|
|
|
/**
|
|
* 공식이 속한 모델
|
|
*/
|
|
public function designModel()
|
|
{
|
|
return $this->belongsTo(DesignModel::class, 'model_id');
|
|
}
|
|
|
|
/**
|
|
* 공식에서 변수 추출
|
|
*/
|
|
public function extractVariables(): array
|
|
{
|
|
// 간단한 변수 추출 (영문자로 시작하는 단어들)
|
|
preg_match_all('/\b[a-zA-Z][a-zA-Z0-9_]*\b/', $this->formula_expression, $matches);
|
|
|
|
// 수학 함수 제외
|
|
$mathFunctions = ['sin', 'cos', 'tan', 'log', 'exp', 'sqrt', 'pow', 'abs', 'ceil', 'floor', 'round', 'max', 'min'];
|
|
$variables = array_diff($matches[0], $mathFunctions);
|
|
|
|
return array_unique($variables);
|
|
}
|
|
|
|
/**
|
|
* 공식 계산 (안전한 eval 대신 간단한 파서 사용)
|
|
*/
|
|
public function calculate(array $values): float
|
|
{
|
|
$expression = $this->formula_expression;
|
|
|
|
// 변수를 값으로 치환
|
|
foreach ($values as $variable => $value) {
|
|
$expression = str_replace($variable, (string) $value, $expression);
|
|
}
|
|
|
|
// 간단한 수식 계산 (보안상 eval 사용 금지)
|
|
return $this->evaluateSimpleExpression($expression);
|
|
}
|
|
|
|
/**
|
|
* 간단한 수식 계산기 (덧셈, 뺄셈, 곱셈, 나눗셈)
|
|
*/
|
|
private function evaluateSimpleExpression(string $expression): float
|
|
{
|
|
// 공백 제거
|
|
$expression = preg_replace('/\s+/', '', $expression);
|
|
|
|
// 간단한 사칙연산만 허용
|
|
if (!preg_match('/^[0-9+\-*\/\(\)\.]+$/', $expression)) {
|
|
throw new \InvalidArgumentException('Invalid expression: ' . $expression);
|
|
}
|
|
|
|
// 안전한 계산을 위해 제한된 연산만 허용
|
|
try {
|
|
// 실제 프로덕션에서는 더 안전한 수식 파서 라이브러리 사용 권장
|
|
return (float) eval("return $expression;");
|
|
} catch (\Throwable $e) {
|
|
throw new \InvalidArgumentException('Formula calculation error: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 의존성 순환 체크
|
|
*/
|
|
public function hasCircularDependency(array $allFormulas): bool
|
|
{
|
|
$visited = [];
|
|
$recursionStack = [];
|
|
|
|
return $this->dfsCheckCircular($allFormulas, $visited, $recursionStack);
|
|
}
|
|
|
|
private function dfsCheckCircular(array $allFormulas, array &$visited, array &$recursionStack): bool
|
|
{
|
|
$visited[$this->formula_name] = true;
|
|
$recursionStack[$this->formula_name] = true;
|
|
|
|
foreach ($this->dependencies as $dependency) {
|
|
if (!isset($visited[$dependency])) {
|
|
$dependentFormula = collect($allFormulas)->firstWhere('formula_name', $dependency);
|
|
if ($dependentFormula && $dependentFormula->dfsCheckCircular($allFormulas, $visited, $recursionStack)) {
|
|
return true;
|
|
}
|
|
} elseif (isset($recursionStack[$dependency])) {
|
|
return true; // 순환 의존성 발견
|
|
}
|
|
}
|
|
|
|
unset($recursionStack[$this->formula_name]);
|
|
return false;
|
|
}
|
|
} |