nfephp-org/sped-common
Biblioteca PHP com classes utilitárias compartilhadas para os projetos SPED da nfephp-org: NFe, CTe, MDFe, e-Financeira, eSfinge e eSocial. Reúne componentes comuns para facilitar integrações e reutilização de código.
nfephp-org/sped-common package is a highly specialized library for Brazilian fiscal electronic documents (NFe, CTe, MDFe, eSocial). It aligns perfectly with Laravel applications requiring SPED compliance, particularly in:
Validator, Keys, Certificate) allow selective adoption. For example, use only the Keys class for key generation without XML validation.composer require nfephp-org/sped-common. No conflicts with Laravel’s core or Blade templates.DOMDocument/SimpleXML, avoiding dependencies on external libraries.getNFeKeyAttribute()). Example:
// Model: NFe.php
public function getKeyAttribute($value) {
return Keys::build($this->cUF, $this->ano, $this->cnpj, ...);
}
storage/app/encrypted) or AWS KMS.Http client to send validated XML to SEFAZ endpoints:
$response = Http::post('https://homologacao.nfe.fazenda.gov.br/ws/nfe-ws/v3.00/autorizacao', [
'body' => $validatedXml,
'headers' => ['Content-Type' => 'application/xml'],
]);
Strings::clearXml() to prevent injection attacks.| Risk Area | Mitigation Strategy |
|---|---|
| Certificate Management | Store certificates in encrypted storage (e.g., Laravel Filesystem + encryption) or hardware security modules (HSMs). Rotate keys using Laravel’s scheduler and the PrivateKey class. |
| XSD Schema Updates | Monitor NFePHP releases for schema changes. Test updates in a staging environment before production deployment. |
| Performance | Validate large XML files asynchronously using Laravel Queues. Example: |
```php
dispatch(new ValidateXmlJob($xmlContent, $xsdPath));
```
Optimize by **caching validation results** for identical payloads. |
| Regulatory Compliance | Implement audit logging for all SPED operations (e.g., signed_at, validator_version, user_id). Use Laravel’s log channels or a dedicated sped_audit table. |
| Deprecation | Fork the package if upstream maintenance declines. The active community (106 stars, 4.8 avg score) reduces this risk, but document dependencies in composer.json to track updates. |
| Cryptographic Failures | Use try-catch blocks for all PrivateKey::sign() and Validator::isValid() calls. Log failures to Sentry or Laravel Horizon for real-time alerts. |
Regulatory Scope:
Security:
Performance:
Validator::isValid() with 1MB+ files to identify bottlenecks.Team Skills:
PrivateKey::sign() and PublicKey::verify().Extensibility:
Validator class or create a wrapper.xmlschema PHP library for broader use cases.Laravel Ecosystem:
// app/Providers/SpedServiceProvider.php
public function register() {
$this->app->singleton(Validator::class, function ($app) {
return new Validator();
});
$this->app->singleton(Keys::class);
}
// app/Facades/Sped.php
public static function validateNFe($xml, $xsdPath = null) {
return Validator::isValid($xml, $xsdPath ?? __DIR__.'/xsd/nfe_v4.00.xsd');
}
// app/Console/Commands/GenerateNFeKey.php
public function handle() {
$key = Keys::build(
$this->argument('cUF'),
$this->argument('ano'),
$this->argument('cnpj'),
// ... other params
);
$this->info("Generated NFe Key: {$key}");
}
Database:
CREATE TABLE nfe_keys (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
key_hash VARCHAR(64) UNIQUE,
cnpj VARCHAR(14) NOT NULL,
valid_from DATE NOT NULL,
valid_to DATE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE certificates (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
certificate_content TEXT NOT NULL,
expiry_date DATE NOT NULL,
is_active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
// app/Models/NFeKey.php
class NFeKey extends Model {
protected $fillable = ['key_hash', 'cnpj', 'valid_from', 'valid_to'];
How can I help you explore Laravel packages today?