laravel 5添加了未找到的外部类

时间:2017-10-29 09:06:53

标签: php laravel

我有一个php lib,它是一组函数,这里是

<?php

# Copyright (c) 2010-2011 Arnaud Renevier, Inc, published under the modified BSD
# license.

namespace App\Gislib;
abstract class CustomException extends \Exception {
    protected $message;
    public function __toString() {
        return get_class($this) . " {$this->message} in {$this->file}({$this->line})\n{$this->getTraceAsString()}";
    }
}

class Unimplemented extends CustomException {
    public function __construct($message) {
        $this->message = "unimplemented $message";
    }
}

class UnimplementedMethod extends Unimplemented {
    public function __construct($method, $class) {
        $this->message = "method {$this->class}::{$this->method}";
    }
}

class InvalidText extends CustomException {
    public function __construct($decoder_name, $text = "") {
        $this->message =  "invalid text for decoder " . $decoder_name . ($text ? (": " . $text) : "");
    }
}

class InvalidFeature extends CustomException {
    public function __construct($decoder_name, $text = "") {
        $this->message =  "invalid feature for decoder $decoder_name" . ($text ? ": $text" : "");
    }
}

abstract class OutOfRangeCoord extends CustomException {
    private $coord;
    public $type;

    public function __construct($coord) {
        $this->message = "invalid {$this->type}: $coord";
    }
}
class OutOfRangeLon extends outOfRangeCoord {
    public $type = "longitude";
}
class OutOfRangeLat extends outOfRangeCoord {
    public $type = "latitude";
}

class UnavailableResource extends CustomException {
    public function __construct($ressource) {
        $this->message = "unavailable ressource: $ressource";
    }
}

interface iDecoder {
    /*
     * @param string $text
     * @return Geometry
     */
    static public function geomFromText($text);
}

abstract class Decoder implements iDecoder {
    static public function geomFromText($text) {
        throw new UnimplementedMethod(__FUNCTION__, get_called_class());
    }
}

interface iGeometry {
    /*
     * @return string
     */
    public function toGeoJSON();

    /*
     * @return string
     */
    public function toKML();

    /*
     * @return string
     */
    public function toWKT();

    /*
     * @param mode: trkseg, rte or wpt
     * @return string
     */
    public function toGPX($mode = null);

    /*
     * @param Geometry $geom
     * @return boolean
     */
    public function equals(Geometry $geom);
}

abstract class Geometry implements iGeometry {
    const name = "";

    public function toGeoJSON() {
        throw new UnimplementedMethod(__FUNCTION__, get_called_class());
    }

    public function toKML() {
        throw new UnimplementedMethod(__FUNCTION__, get_called_class());
    }

    public function toGPX($mode = null) {
        throw new UnimplementedMethod(__FUNCTION__, get_called_class());
    }

    public function toWKT() {
        throw new UnimplementedMethod(__FUNCTION__, get_called_class());
    }

    public function equals(Geometry $geom) {
        throw new UnimplementedMethod(__FUNCTION__, get_called_class());
    }

    public function __toString() {
        return $this->toWKT();
    }
}

class GeoJSON extends Decoder {

    static public function geomFromText($text) {
        $ltext = strtolower($text);
        $obj = json_decode($ltext);
        if (is_null ($obj)) {
            throw new InvalidText(__CLASS__, $text);
        }

        try {
            $geom = static::_geomFromJson($obj);
        } catch(InvalidText $e) {
            throw new InvalidText(__CLASS__, $text);
        } catch(\Exception $e) {
            throw $e;
        }

        return $geom;
    }

    static protected function _geomFromJson($json) {
        if (property_exists ($json, "geometry") and is_object($json->geometry)) {
            return static::_geomFromJson($json->geometry);
        }

        if (!property_exists ($json, "type") or !is_string($json->type)) {
            throw new InvalidText(__CLASS__);
        }

        foreach (array("Point", "MultiPoint", "LineString", "MultiLinestring", "LinearRing",
                       "Polygon", "MultiPolygon", "GeometryCollection") as $json_type) {
            if (strtolower($json_type) == $json->type) {
                $type = $json_type;
                break;
            }
        }

        if (!isset($type)) {
            throw new InvalidText(__CLASS__);
        }

        try {
            $components = call_user_func(array('static', 'parse'.$type), $json);
        } catch(InvalidText $e) {
            throw new InvalidText(__CLASS__);
        } catch(\Exception $e) {
            throw $e;
        }

        $constructor = __NAMESPACE__ . '\\' . $type;
        return new $constructor($components);
    }

   static protected function parsePoint($json) {
        if (!property_exists ($json, "coordinates") or !is_array($json->coordinates)) {
            throw new InvalidText(__CLASS__);
        }
        return $json->coordinates;
    }

    static protected function parseMultiPoint($json) {
        if (!property_exists ($json, "coordinates") or !is_array($json->coordinates)) {
            throw new InvalidText(__CLASS__);
        }
        return array_map(function($coords) {
            return new Point($coords);
        }, $json->coordinates);
    }

    static protected function parseLineString($json) {
        return static::parseMultiPoint($json);
    }

    static protected function parseMultiLineString($json) {
        $components = array();
        if (!property_exists ($json, "coordinates") or !is_array($json->coordinates)) {
            throw new InvalidText(__CLASS__);
        }
        foreach ($json->coordinates as $coordinates) {
            $linecomp = array();
            foreach ($coordinates as $coordinates) {
                $linecomp[] = new Point($coordinates);
            }
            $components[] = new LineString($linecomp);
        }
        return $components;
    }

    static protected function parseLinearRing($json) {
        return static::parseMultiPoint($json);
    }

    static protected function parsePolygon($json) {
        $components = array();
        if (!property_exists ($json, "coordinates") or !is_array($json->coordinates)) {
            throw new InvalidText(__CLASS__);
        }
        foreach ($json->coordinates as $coordinates) {
            $ringcomp = array();
            foreach ($coordinates as $coordinates) {
                $ringcomp[] = new Point($coordinates);
            }
            $components[] = new LinearRing($ringcomp);
        }
        return $components;
    }

    static protected function parseMultiPolygon($json) {
        $components = array();
        if (!property_exists ($json, "coordinates") or !is_array($json->coordinates)) {
            throw new InvalidText(__CLASS__);
        }
        foreach ($json->coordinates as $coordinates) {
            $polycomp = array();
            foreach ($coordinates as $coordinates) {
                $ringcomp = array();
                foreach ($coordinates as $coordinates) {
                    $ringcomp[] = new Point($coordinates);
                }
                $polycomp[] = new LinearRing($ringcomp);
            }
            $components[] = new Polygon($polycomp);
        }
        return $components;
    }

    static protected function parseGeometryCollection($json) {
        if (!property_exists ($json, "geometries") or !is_array($json->geometries)) {
            throw new InvalidText(__CLASS__);
        }
        $components = array();
        foreach ($json->geometries as $geometry) {
            $components[] = static::_geomFromJson($geometry);
        }

        return $components;
    }

}}

我已将其放入App\Gislib\Gislib.php 在我的控制器中,我已将其使用添加为use App\Gislib\GeoJSON;,但当我尝试加载其类$decoder =new \App\Gislib\GeoJSON();时,它说Class 'App\Gislib\GeoJSON' not found我的错误在哪里?它是否与扩展类型或命名空间有关?我知道有一些其他方法可以调用这些类,但我可以使用命名空间加载它们 感谢

2 个答案:

答案 0 :(得分:2)

每个类或接口都需要在自己的文件中,文件名与类名匹配。

例如,在app/Gislib/Unimplemented.php

<?php

namespace App\Gislib;

class Unimplemented extends CustomException {
    public function __construct($message) {
        $this->message = "unimplemented $message";
    }
}

然后在app/Gislib/iDecoder.php

<?php

namespace App\Gislib;

interface iDecoder {
    /*
     * @param string $text
     * @return Geometry
     */
    static public function geomFromText($text);
}

这是由于Laravel遵循PSR-4标准。

如果在分割文件后仍然出现错误,请尝试运行composer dump

答案 1 :(得分:2)

使用PSR-4自动加载,要求类名与文件名匹配。见https://stackoverflow.com/a/29033779

但你可以做的是像这样修改你的composer.json文件:

"autoload": {
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "files" : [
      "app/Gislib/Gislib.php"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

添加为“文件”部分并提供lib的路径。 (我认为你必须在那之后做作曲家转储自动加载。现在它应该可以工作。