[关闭]
@leptune 2024-08-04T19:07:17.000000Z 字数 2238 阅读 78

好用的路径alias

linux


  1. alias pma='php D:/.pm-php/add.php'
  2. alias pmr='php D:/.pm-php/rename.php'
  3. alias pmd='php D:/.pm-php/del.php'
  4. alias pml='php D:/.pm-php/list.php'
  5. function pmg() {
  6. p=`php D:/.pm-php/go.php $1`
  7. if [[ $? == 0 ]]; then
  8. cd $p;
  9. echo 'succes!';
  10. pwd
  11. else
  12. echo $p;
  13. fi
  14. }

pm实现源码:

add.php

  1. <?php
  2. if (count($argv) !== 2) {
  3. echo 'error: Usage: pma name'.PHP_EOL;
  4. exit(1);
  5. }
  6. $name = $argv[1];
  7. $cwd = getcwd();
  8. $data = [];
  9. $savePath = __DIR__ . '/' . '.pm.data.seri';
  10. if (file_exists($savePath)) {
  11. $data = unserialize(file_get_contents($savePath));
  12. }
  13. $data[$name] = $cwd;
  14. file_put_contents($savePath, serialize($data));
  15. echo 'success!'.PHP_EOL;
  16. exit(0);

go.php

  1. <?php
  2. if (count($argv) !== 2) {
  3. echo 'error: Usage: pmg name'.PHP_EOL;
  4. exit(1);
  5. }
  6. $name = $argv[1];
  7. $data = [];
  8. $savePath = __DIR__ . '/' . '.pm.data.seri';
  9. if (file_exists($savePath)) {
  10. $data = unserialize(file_get_contents($savePath));
  11. }
  12. if (!isset($data[$name])) {
  13. echo "error: $name not exist! add first!".PHP_EOL;
  14. exit(1);
  15. }
  16. if (!file_exists($data[$name])) {
  17. echo "error: path [{$data[$name]}] not exist, please check!".PHP_EOL;
  18. exit(1);
  19. }
  20. echo $data[$name];
  21. exit(0);

list.php

  1. <?php
  2. if (count($argv) !== 1) {
  3. echo 'error: Usage: pml'.PHP_EOL;
  4. exit(1);
  5. }
  6. $data = [];
  7. $savePath = __DIR__ . '/' . '.pm.data.seri';
  8. if (file_exists($savePath)) {
  9. $data = unserialize(file_get_contents($savePath));
  10. }
  11. if (empty($data)) {
  12. echo 'error: there is no path alias! please add first!'.PHP_EOL;
  13. exit(1);
  14. }
  15. echo PHP_EOL;
  16. foreach ($data as $name => $path) {
  17. echo "$name => $path".PHP_EOL;
  18. }
  19. echo PHP_EOL;
  20. echo 'success!'.PHP_EOL;
  21. exit(0);

del.php

  1. <?php
  2. if (count($argv) !== 2) {
  3. echo 'error: Usage: pmd name'.PHP_EOL;
  4. exit(1);
  5. }
  6. $name = $argv[1];
  7. $data = [];
  8. $savePath = __DIR__ . '/' . '.pm.data.seri';
  9. if (file_exists($savePath)) {
  10. $data = unserialize(file_get_contents($savePath));
  11. }
  12. if (!isset($data[$name])) {
  13. echo "error: $name not exist! add first!".PHP_EOL;
  14. exit(1);
  15. }
  16. unset($data[$name]);
  17. file_put_contents($savePath, serialize($data));
  18. echo 'success!'.PHP_EOL;
  19. exit(0);

rename.php

  1. <?php
  2. if (count($argv) !== 3) {
  3. echo 'error: Usage: pmr old_name new_name'.PHP_EOL;
  4. exit(1);
  5. }
  6. $old_name = $argv[1];
  7. $new_name = $argv[2];
  8. $data = [];
  9. $savePath = __DIR__ . '/' . '.pm.data.seri';
  10. if (file_exists($savePath)) {
  11. $data = unserialize(file_get_contents($savePath));
  12. }
  13. if (!isset($data[$old_name])) {
  14. echo "error: $old_name not exist! add first!".PHP_EOL;
  15. exit(1);
  16. }
  17. $data[$new_name] = $data[$old_name];
  18. unset($data[$old_name]);
  19. file_put_contents($savePath, serialize($data));
  20. echo 'success!'.PHP_EOL;
  21. exit(0);
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注