| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace PhpParser; |
| 4: | |
| 5: | use PhpParser\Node\Arg; |
| 6: | use PhpParser\Node\Expr; |
| 7: | use PhpParser\Node\Expr\BinaryOp\Concat; |
| 8: | use PhpParser\Node\Identifier; |
| 9: | use PhpParser\Node\Name; |
| 10: | use PhpParser\Node\Scalar\String_; |
| 11: | use PhpParser\Node\Stmt\Use_; |
| 12: | |
| 13: | class BuilderFactory { |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | public function attribute($name, array $args = []): Node\Attribute { |
| 21: | return new Node\Attribute( |
| 22: | BuilderHelpers::normalizeName($name), |
| 23: | $this->args($args) |
| 24: | ); |
| 25: | } |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | public function namespace($name): Builder\Namespace_ { |
| 35: | return new Builder\Namespace_($name); |
| 36: | } |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | public function class(string $name): Builder\Class_ { |
| 46: | return new Builder\Class_($name); |
| 47: | } |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | public function interface(string $name): Builder\Interface_ { |
| 57: | return new Builder\Interface_($name); |
| 58: | } |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | public function trait(string $name): Builder\Trait_ { |
| 68: | return new Builder\Trait_($name); |
| 69: | } |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | public function enum(string $name): Builder\Enum_ { |
| 79: | return new Builder\Enum_($name); |
| 80: | } |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | public function useTrait(...$traits): Builder\TraitUse { |
| 90: | return new Builder\TraitUse(...$traits); |
| 91: | } |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | public function traitUseAdaptation($trait, $method = null): Builder\TraitUseAdaptation { |
| 102: | if ($method === null) { |
| 103: | $method = $trait; |
| 104: | $trait = null; |
| 105: | } |
| 106: | |
| 107: | return new Builder\TraitUseAdaptation($trait, $method); |
| 108: | } |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | public function method(string $name): Builder\Method { |
| 118: | return new Builder\Method($name); |
| 119: | } |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | public function param(string $name): Builder\Param { |
| 129: | return new Builder\Param($name); |
| 130: | } |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | public function property(string $name): Builder\Property { |
| 140: | return new Builder\Property($name); |
| 141: | } |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | public function function(string $name): Builder\Function_ { |
| 151: | return new Builder\Function_($name); |
| 152: | } |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | public function use($name): Builder\Use_ { |
| 162: | return new Builder\Use_($name, Use_::TYPE_NORMAL); |
| 163: | } |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | public function useFunction($name): Builder\Use_ { |
| 173: | return new Builder\Use_($name, Use_::TYPE_FUNCTION); |
| 174: | } |
| 175: | |
| 176: | |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | public function useConst($name): Builder\Use_ { |
| 184: | return new Builder\Use_($name, Use_::TYPE_CONSTANT); |
| 185: | } |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | public function classConst($name, $value): Builder\ClassConst { |
| 196: | return new Builder\ClassConst($name, $value); |
| 197: | } |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: | public function enumCase($name): Builder\EnumCase { |
| 207: | return new Builder\EnumCase($name); |
| 208: | } |
| 209: | |
| 210: | |
| 211: | |
| 212: | |
| 213: | |
| 214: | |
| 215: | public function val($value): Expr { |
| 216: | return BuilderHelpers::normalizeValue($value); |
| 217: | } |
| 218: | |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | public function var($name): Expr\Variable { |
| 225: | if (!\is_string($name) && !$name instanceof Expr) { |
| 226: | throw new \LogicException('Variable name must be string or Expr'); |
| 227: | } |
| 228: | |
| 229: | return new Expr\Variable($name); |
| 230: | } |
| 231: | |
| 232: | |
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: | |
| 238: | |
| 239: | |
| 240: | |
| 241: | public function args(array $args): array { |
| 242: | $normalizedArgs = []; |
| 243: | foreach ($args as $key => $arg) { |
| 244: | if (!($arg instanceof Arg)) { |
| 245: | $arg = new Arg(BuilderHelpers::normalizeValue($arg)); |
| 246: | } |
| 247: | if (\is_string($key)) { |
| 248: | $arg->name = BuilderHelpers::normalizeIdentifier($key); |
| 249: | } |
| 250: | $normalizedArgs[] = $arg; |
| 251: | } |
| 252: | return $normalizedArgs; |
| 253: | } |
| 254: | |
| 255: | |
| 256: | |
| 257: | |
| 258: | |
| 259: | |
| 260: | |
| 261: | public function funcCall($name, array $args = []): Expr\FuncCall { |
| 262: | return new Expr\FuncCall( |
| 263: | BuilderHelpers::normalizeNameOrExpr($name), |
| 264: | $this->args($args) |
| 265: | ); |
| 266: | } |
| 267: | |
| 268: | |
| 269: | |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 274: | |
| 275: | public function methodCall(Expr $var, $name, array $args = []): Expr\MethodCall { |
| 276: | return new Expr\MethodCall( |
| 277: | $var, |
| 278: | BuilderHelpers::normalizeIdentifierOrExpr($name), |
| 279: | $this->args($args) |
| 280: | ); |
| 281: | } |
| 282: | |
| 283: | |
| 284: | |
| 285: | |
| 286: | |
| 287: | |
| 288: | |
| 289: | |
| 290: | public function staticCall($class, $name, array $args = []): Expr\StaticCall { |
| 291: | return new Expr\StaticCall( |
| 292: | BuilderHelpers::normalizeNameOrExpr($class), |
| 293: | BuilderHelpers::normalizeIdentifierOrExpr($name), |
| 294: | $this->args($args) |
| 295: | ); |
| 296: | } |
| 297: | |
| 298: | |
| 299: | |
| 300: | |
| 301: | |
| 302: | |
| 303: | |
| 304: | public function new($class, array $args = []): Expr\New_ { |
| 305: | return new Expr\New_( |
| 306: | BuilderHelpers::normalizeNameOrExpr($class), |
| 307: | $this->args($args) |
| 308: | ); |
| 309: | } |
| 310: | |
| 311: | |
| 312: | |
| 313: | |
| 314: | |
| 315: | |
| 316: | public function constFetch($name): Expr\ConstFetch { |
| 317: | return new Expr\ConstFetch(BuilderHelpers::normalizeName($name)); |
| 318: | } |
| 319: | |
| 320: | |
| 321: | |
| 322: | |
| 323: | |
| 324: | |
| 325: | |
| 326: | public function propertyFetch(Expr $var, $name): Expr\PropertyFetch { |
| 327: | return new Expr\PropertyFetch($var, BuilderHelpers::normalizeIdentifierOrExpr($name)); |
| 328: | } |
| 329: | |
| 330: | |
| 331: | |
| 332: | |
| 333: | |
| 334: | |
| 335: | |
| 336: | public function classConstFetch($class, $name): Expr\ClassConstFetch { |
| 337: | return new Expr\ClassConstFetch( |
| 338: | BuilderHelpers::normalizeNameOrExpr($class), |
| 339: | BuilderHelpers::normalizeIdentifierOrExpr($name) |
| 340: | ); |
| 341: | } |
| 342: | |
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: | |
| 348: | public function concat(...$exprs): Concat { |
| 349: | $numExprs = count($exprs); |
| 350: | if ($numExprs < 2) { |
| 351: | throw new \LogicException('Expected at least two expressions'); |
| 352: | } |
| 353: | |
| 354: | $lastConcat = $this->normalizeStringExpr($exprs[0]); |
| 355: | for ($i = 1; $i < $numExprs; $i++) { |
| 356: | $lastConcat = new Concat($lastConcat, $this->normalizeStringExpr($exprs[$i])); |
| 357: | } |
| 358: | return $lastConcat; |
| 359: | } |
| 360: | |
| 361: | |
| 362: | |
| 363: | |
| 364: | private function normalizeStringExpr($expr): Expr { |
| 365: | if ($expr instanceof Expr) { |
| 366: | return $expr; |
| 367: | } |
| 368: | |
| 369: | if (\is_string($expr)) { |
| 370: | return new String_($expr); |
| 371: | } |
| 372: | |
| 373: | throw new \LogicException('Expected string or Expr'); |
| 374: | } |
| 375: | } |
| 376: | |