@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Slightly improve organization of PhabricatorApplicationEditEngine

Summary: Ref T9132. This just moves code around, breaks it up into some smaller chunks, tries to reduce duplication, and adds a touch of documentation.

Test Plan: Created and edited pastes.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T9132

Differential Revision: https://secure.phabricator.com/D14398

+296 -140
+296 -140
src/applications/transactions/editengine/PhabricatorApplicationEditEngine.php
··· 2 2 3 3 4 4 /** 5 + * @task fields Managing Fields 6 + * @task text Display Text 7 + * @task uri Managing URIs 8 + * @task load Creating and Loading Objects 5 9 * @task web Responding to Web Requests 10 + * @task edit Responding to Edit Requests 11 + * @task http Responding to HTTP Parameter Requests 6 12 * @task conduit Responding to Conduit Requests 7 13 */ 8 14 abstract class PhabricatorApplicationEditEngine extends Phobject { ··· 29 35 final public function getController() { 30 36 return $this->controller; 31 37 } 38 + 39 + 40 + /* -( Managing Fields )---------------------------------------------------- */ 41 + 42 + 43 + abstract protected function buildCustomEditFields($object); 32 44 33 45 final protected function buildEditFields($object) { 34 46 $viewer = $this->getViewer(); ··· 176 188 } 177 189 } 178 190 191 + foreach ($fields as $field) { 192 + $field 193 + ->setViewer($viewer) 194 + ->setObject($object); 195 + } 196 + 179 197 return $fields; 180 198 } 181 199 182 - abstract protected function newEditableObject(); 183 - abstract protected function newObjectQuery(); 184 - abstract protected function buildCustomEditFields($object); 200 + 201 + /* -( Display Text )------------------------------------------------------- */ 185 202 203 + 204 + /** 205 + * @task text 206 + */ 186 207 abstract protected function getObjectCreateTitleText($object); 208 + 209 + 210 + /** 211 + * @task text 212 + */ 187 213 abstract protected function getObjectEditTitleText($object); 214 + 215 + 216 + /** 217 + * @task text 218 + */ 188 219 abstract protected function getObjectCreateShortText($object); 220 + 221 + 222 + /** 223 + * @task text 224 + */ 189 225 abstract protected function getObjectEditShortText($object); 226 + 227 + 228 + /** 229 + * @task text 230 + */ 231 + protected function getObjectCreateButtonText($object) { 232 + return $this->getObjectCreateTitleText($object); 233 + } 234 + 235 + 236 + /** 237 + * @task text 238 + */ 239 + protected function getObjectEditButtonText($object) { 240 + return pht('Save Changes'); 241 + } 242 + 243 + 244 + /* -( Managing URIs )------------------------------------------------------ */ 245 + 246 + 247 + /** 248 + * @task uri 249 + */ 190 250 abstract protected function getObjectViewURI($object); 191 251 252 + 253 + /** 254 + * @task uri 255 + */ 192 256 protected function getObjectEditURI($object) { 193 257 return $this->getController()->getApplicationURI('edit/'); 194 258 } 195 259 260 + 261 + /** 262 + * @task uri 263 + */ 196 264 protected function getObjectCreateCancelURI($object) { 197 265 return $this->getController()->getApplicationURI(); 198 266 } 199 267 268 + 269 + /** 270 + * @task uri 271 + */ 200 272 protected function getObjectEditCancelURI($object) { 201 273 return $this->getObjectViewURI($object); 202 274 } 203 275 204 - protected function getObjectCreateButtonText($object) { 205 - return $this->getObjectCreateTitleText($object); 206 - } 207 276 208 - protected function getObjectEditButtonText($object) { 209 - return pht('Save Changes'); 210 - } 211 - 277 + /** 278 + * @task uri 279 + */ 212 280 protected function getEditURI($object, $path = null) { 213 281 $parts = array( 214 282 $this->getObjectEditURI($object), ··· 225 293 return implode('', $parts); 226 294 } 227 295 228 - final protected function setIsCreate($is_create) { 296 + 297 + /* -( Creating and Loading Objects )--------------------------------------- */ 298 + 299 + 300 + /** 301 + * Initialize a new object for creation. 302 + * 303 + * @return object Newly initialized object. 304 + * @task load 305 + */ 306 + abstract protected function newEditableObject(); 307 + 308 + 309 + /** 310 + * Build an empty query for objects. 311 + * 312 + * @return PhabricatorPolicyAwareQuery Query. 313 + * @task load 314 + */ 315 + abstract protected function newObjectQuery(); 316 + 317 + 318 + /** 319 + * Test if this workflow is creating a new object or editing an existing one. 320 + * 321 + * @return bool True if a new object is being created. 322 + * @task load 323 + */ 324 + final protected function getIsCreate() { 325 + return $this->isCreate; 326 + } 327 + 328 + 329 + /** 330 + * Flag this workflow as a create or edit. 331 + * 332 + * @param bool True if this is a create workflow. 333 + * @return this 334 + * @task load 335 + */ 336 + private function setIsCreate($is_create) { 229 337 $this->isCreate = $is_create; 230 338 return $this; 231 339 } 232 340 233 - final protected function getIsCreate() { 234 - return $this->isCreate; 341 + 342 + /** 343 + * Load an object by ID. 344 + * 345 + * @param int Object ID. 346 + * @return object|null Object, or null if no such object exists. 347 + * @task load 348 + */ 349 + private function newObjectFromID($id) { 350 + $query = $this->newObjectQuery() 351 + ->withIDs(array($id)); 352 + 353 + return $this->newObjectFromQuery($query); 354 + } 355 + 356 + 357 + /** 358 + * Load an object by PHID. 359 + * 360 + * @param phid Object PHID. 361 + * @return object|null Object, or null if no such object exists. 362 + * @task load 363 + */ 364 + private function newObjectFromPHID($phid) { 365 + $query = $this->newObjectQuery() 366 + ->withPHIDs(array($phid)); 367 + 368 + return $this->newObjectFromQuery($query); 369 + } 370 + 371 + 372 + /** 373 + * Load an object given a configured query. 374 + * 375 + * @param PhabricatorPolicyAwareQuery Configured query. 376 + * @return object|null Object, or null if no such object exists. 377 + * @task load 378 + */ 379 + private function newObjectFromQuery(PhabricatorPolicyAwareQuery $query) { 380 + $viewer = $this->getViewer(); 381 + 382 + $object = $query 383 + ->setViewer($viewer) 384 + ->requireCapabilities( 385 + array( 386 + PhabricatorPolicyCapability::CAN_VIEW, 387 + PhabricatorPolicyCapability::CAN_EDIT, 388 + )) 389 + ->executeOne(); 390 + if (!$object) { 391 + return null; 392 + } 393 + 394 + return $object; 235 395 } 236 396 397 + 398 + /* -( Responding to Web Requests )----------------------------------------- */ 399 + 400 + 237 401 final public function buildResponse() { 238 - $controller = $this->getController(); 239 402 $viewer = $this->getViewer(); 403 + $controller = $this->getController(); 240 404 $request = $controller->getRequest(); 241 405 242 406 $id = $request->getURIData('id'); 243 407 if ($id) { 244 - $object = $this->newObjectQuery() 245 - ->setViewer($viewer) 246 - ->withIDs(array($id)) 247 - ->requireCapabilities( 248 - array( 249 - PhabricatorPolicyCapability::CAN_VIEW, 250 - PhabricatorPolicyCapability::CAN_EDIT, 251 - )) 252 - ->executeOne(); 408 + $this->setIsCreate(false); 409 + $object = $this->newObjectFromID($id); 253 410 if (!$object) { 254 411 return new Aphront404Response(); 255 412 } 256 - $this->setIsCreate(false); 257 413 } else { 414 + $this->setIsCreate(true); 258 415 $object = $this->newEditableObject(); 259 - $this->setIsCreate(true); 260 - } 261 - 262 - $fields = $this->buildEditFields($object); 263 - 264 - foreach ($fields as $field) { 265 - $field 266 - ->setViewer($viewer) 267 - ->setObject($object); 268 416 } 269 417 270 418 $action = $request->getURIData('editAction'); 271 419 switch ($action) { 272 420 case 'parameters': 273 - return $this->buildParametersResponse($object, $fields); 421 + return $this->buildParametersResponse($object); 422 + default: 423 + return $this->buildEditResponse($object); 274 424 } 425 + } 426 + 427 + private function buildCrumbs($object, $final = false) { 428 + $controller = $this->getcontroller(); 429 + 430 + $crumbs = $controller->buildApplicationCrumbsForEditEngine(); 431 + if ($this->getIsCreate()) { 432 + $create_text = $this->getObjectCreateShortText($object); 433 + if ($final) { 434 + $crumbs->addTextCrumb($create_text); 435 + } else { 436 + $edit_uri = $this->getEditURI($object); 437 + $crumbs->addTextCrumb($create_text, $edit_uri); 438 + } 439 + } else { 440 + $crumbs->addTextCrumb( 441 + $this->getObjectEditShortText($object), 442 + $this->getObjectViewURI($object)); 443 + 444 + $edit_text = pht('Edit'); 445 + if ($final) { 446 + $crumbs->addTextCrumb($edit_text); 447 + } else { 448 + $edit_uri = $this->getEditURI($object); 449 + $crumbs->addTextCrumb($edit_text, $edit_uri); 450 + } 451 + } 452 + 453 + return $crumbs; 454 + } 455 + 456 + private function buildEditResponse($object) { 457 + $viewer = $this->getViewer(); 458 + $controller = $this->getController(); 459 + $request = $controller->getRequest(); 460 + 461 + $fields = $this->buildEditFields($object); 462 + $template = $object->getApplicationTransactionTemplate(); 275 463 276 464 $validation_exception = null; 277 465 if ($request->isFormPost()) { 278 466 foreach ($fields as $field) { 279 467 $field->readValueFromSubmit($request); 280 468 } 281 - 282 - $template = $object->getApplicationTransactionTemplate(); 283 469 284 470 $xactions = array(); 285 471 foreach ($fields as $field) { ··· 289 475 $editor = $object->getApplicationTransactionEditor() 290 476 ->setActor($viewer) 291 477 ->setContentSourceFromRequest($request) 292 - ->setContinueOnNoEffect(true) 293 - ->setContinueOnMissingFields(false); 478 + ->setContinueOnNoEffect(true); 294 479 295 480 try { 296 481 ··· 313 498 } 314 499 } 315 500 316 - $box = id(new PHUIObjectBoxView()) 317 - ->setUser($viewer); 318 - 319 - $crumbs = $this->buildCrumbs($object, $final = true); 501 + $action_button = $this->buildEditFormActionButton($object); 320 502 321 503 if ($this->getIsCreate()) { 322 504 $header_text = $this->getObjectCreateTitleText($object); 323 - 324 - $cancel_uri = $this->getObjectCreateCancelURI($object); 325 - $submit_button = $this->getObjectCreateButtonText($object); 326 505 } else { 327 506 $header_text = $this->getObjectEditTitleText($object); 328 - 329 - $cancel_uri = $this->getObjectEditCancelURI($object); 330 - $submit_button = $this->getObjectEditButtonText($object); 331 507 } 332 508 333 509 $header = id(new PHUIHeaderView()) 334 - ->setHeader($header_text); 335 - 336 - $action_button = $this->buildEditFormActionButton($object); 337 - 338 - $header->addActionLink($action_button); 339 - 340 - $box->setHeader($header); 510 + ->setHeader($header_text) 511 + ->addActionLink($action_button); 341 512 342 - $form = id(new AphrontFormView()) 343 - ->setUser($viewer); 513 + $crumbs = $this->buildCrumbs($object, $final = true); 514 + $form = $this->buildEditForm($object, $fields); 344 515 345 - foreach ($fields as $field) { 346 - $field->appendToForm($form); 347 - } 348 - 349 - $form->appendControl( 350 - id(new AphrontFormSubmitControl()) 351 - ->addCancelButton($cancel_uri) 352 - ->setValue($submit_button)); 353 - 354 - $box->appendChild($form); 355 - 356 - if ($validation_exception) { 357 - $box->setValidationException($validation_exception); 358 - } 516 + $box = id(new PHUIObjectBoxView()) 517 + ->setUser($viewer) 518 + ->setHeader($header) 519 + ->setValidationException($validation_exception) 520 + ->appendChild($form); 359 521 360 522 return $controller->newPage() 361 523 ->setTitle($header_text) ··· 363 525 ->appendChild($box); 364 526 } 365 527 366 - private function buildParametersResponse($object, array $fields) { 367 - $controller = $this->getController(); 528 + private function buildEditForm($object, array $fields) { 368 529 $viewer = $this->getViewer(); 369 - $request = $controller->getRequest(); 370 - 371 - $crumbs = $this->buildCrumbs($object); 372 - $crumbs->addTextCrumb(pht('HTTP Parameters')); 373 - $crumbs->setBorder(true); 374 - 375 - $header = id(new PHUIHeaderView()) 376 - ->setHeader( 377 - pht( 378 - 'HTTP Parameters: %s', 379 - $this->getObjectCreateShortText($object))); 380 - 381 - $document = id(new PHUIDocumentViewPro()) 382 - ->setUser($viewer) 383 - ->setHeader($header); 384 - 385 - $document->appendChild( 386 - id(new PhabricatorApplicationEditHTTPParameterHelpView()) 387 - ->setUser($viewer) 388 - ->setFields($fields)); 389 530 390 - return $controller->newPage() 391 - ->setTitle(pht('HTTP Parameters')) 392 - ->setCrumbs($crumbs) 393 - ->addClass('pro-white-background') 394 - ->appendChild($document); 395 - } 531 + $form = id(new AphrontFormView()) 532 + ->setUser($viewer); 396 533 397 - private function buildCrumbs($object, $final = false) { 398 - $controller = $this->getcontroller(); 534 + foreach ($fields as $field) { 535 + $field->appendToForm($form); 536 + } 399 537 400 - $crumbs = $controller->buildApplicationCrumbsForEditEngine(); 401 538 if ($this->getIsCreate()) { 402 - $create_text = $this->getObjectCreateShortText($object); 403 - if ($final) { 404 - $crumbs->addTextCrumb($create_text); 405 - } else { 406 - $edit_uri = $this->getEditURI($object); 407 - $crumbs->addTextCrumb($create_text, $edit_uri); 408 - } 539 + $cancel_uri = $this->getObjectCreateCancelURI($object); 540 + $submit_button = $this->getObjectCreateButtonText($object); 409 541 } else { 410 - $crumbs->addTextCrumb( 411 - $this->getObjectEditShortText($object), 412 - $this->getObjectViewURI($object)); 542 + $cancel_uri = $this->getObjectEditCancelURI($object); 543 + $submit_button = $this->getObjectEditButtonText($object); 544 + } 413 545 414 - $edit_text = pht('Edit'); 415 - if ($final) { 416 - $crumbs->addTextCrumb($edit_text); 417 - } else { 418 - $edit_uri = $this->getEditURI($object); 419 - $crumbs->addTextCrumb($edit_text, $edit_uri); 420 - } 421 - } 546 + $form->appendControl( 547 + id(new AphrontFormSubmitControl()) 548 + ->addCancelButton($cancel_uri) 549 + ->setValue($submit_button)); 422 550 423 - return $crumbs; 551 + return $form; 424 552 } 425 553 426 554 private function buildEditFormActionButton($object) { ··· 455 583 } 456 584 457 585 586 + /* -( Responding to HTTP Parameter Requests )------------------------------ */ 587 + 588 + 589 + /** 590 + * Respond to a request for documentation on HTTP parameters. 591 + * 592 + * @param object Editable object. 593 + * @return AphrontResponse Response object. 594 + * @task http 595 + */ 596 + private function buildParametersResponse($object) { 597 + $controller = $this->getController(); 598 + $viewer = $this->getViewer(); 599 + $request = $controller->getRequest(); 600 + $fields = $this->buildEditFields($object); 601 + 602 + $crumbs = $this->buildCrumbs($object); 603 + $crumbs->addTextCrumb(pht('HTTP Parameters')); 604 + $crumbs->setBorder(true); 605 + 606 + $header_text = pht( 607 + 'HTTP Parameters: %s', 608 + $this->getObjectCreateShortText($object)); 609 + 610 + $header = id(new PHUIHeaderView()) 611 + ->setHeader($header_text); 612 + 613 + $help_view = id(new PhabricatorApplicationEditHTTPParameterHelpView()) 614 + ->setUser($viewer) 615 + ->setFields($fields); 616 + 617 + $document = id(new PHUIDocumentViewPro()) 618 + ->setUser($viewer) 619 + ->setHeader($header) 620 + ->appendChild($help_view); 621 + 622 + return $controller->newPage() 623 + ->setTitle(pht('HTTP Parameters')) 624 + ->setCrumbs($crumbs) 625 + ->addClass('pro-white-background') 626 + ->appendChild($document); 627 + } 628 + 629 + 458 630 /* -( Conduit )------------------------------------------------------------ */ 459 631 460 632 ··· 471 643 472 644 $phid = $request->getValue('objectPHID'); 473 645 if ($phid) { 474 - $object = $this->newObjectQuery() 475 - ->setViewer($viewer) 476 - ->withPHIDs(array($phid)) 477 - ->requireCapabilities( 478 - array( 479 - PhabricatorPolicyCapability::CAN_VIEW, 480 - PhabricatorPolicyCapability::CAN_EDIT, 481 - )) 482 - ->executeOne(); 646 + $this->setIsCreate(false); 647 + $object = $this->newObjectFromPHID($phid); 483 648 if (!$object) { 484 649 throw new Exception(pht('No such object with PHID "%s".', $phid)); 485 650 } 486 - $this->setIsCreate(false); 487 651 } else { 652 + $this->setIsCreate(true); 488 653 $object = $this->newEditableObject(); 489 - $this->setIsCreate(true); 490 654 } 491 655 492 656 $fields = $this->buildEditFields($object); 493 - 494 - foreach ($fields as $field) { 495 - $field 496 - ->setViewer($viewer) 497 - ->setObject($object); 498 - } 499 657 500 658 $types = $this->getAllEditTypesFromFields($fields); 501 659 $template = $object->getApplicationTransactionTemplate(); ··· 617 775 $fields = $this->buildEditFields($object); 618 776 return $this->getAllEditTypesFromFields($fields); 619 777 } 620 - 621 - 622 778 623 779 624 780 }