#!/usr/bin/env php
<?php

/*
 * Copyright 2012 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

$root = dirname(dirname(dirname(__FILE__)));
require_once $root.'/scripts/__init_script__.php';

if ($argc !== 2 || $argv[1] === '--help') {
  echo "Usage: aphrontpath.php <url>\n";
  echo "Purpose: Print controller which will process passed <url>.\n";
  exit(1);
}

$url = parse_url($argv[1]);
$path = '/'.(isset($url['path']) ? ltrim($url['path'], '/') : '');

$config_key = 'aphront.default-application-configuration-class';
$config_class = PhabricatorEnv::getEnvConfig($config_key);
$application = newv($config_class, array());
$mapper = new AphrontURIMapper($application->getURIMap());

list($controller) = $mapper->mapPath($path);
if (!$controller && $path[strlen($path) - 1] !== '/') {
  list($controller) = $mapper->mapPath($path.'/');
}
if ($controller) {
  echo "$controller\n";
}

Create script printing controller name based on URL
Create script printing controller name based on URL

Summary:
With this script, I am able to add bookmarklet to my browser which edits the
controller responsible for displaying current page by a single click.
Plus it can be useful also for other uses.

Test Plan:
  ./aphrontpath.php /
  ./aphrontpath.php D123
  ./aphrontpath.php /D123
  ./aphrontpath.php /D123/ # doesn't exist
  ./aphrontpath.php https://secure.phabricator.com/D123
  ./aphrontpath.php https://secure.phabricator.com/D123?x=2
  ./aphrontpath.php https://secure.phabricator.com/D123#comment-1
  ./aphrontpath.php differential
  ./aphrontpath.php /differential/
  ./aphrontpath.php /w/ # rewritten by custom config

Reviewers: epriestley

Reviewed By: epriestley

CC: aran, epriestley

Maniphest Tasks: T349

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